Remove grid, background color, and top and right borders from ggplot2

后端 未结 7 1531
生来不讨喜
生来不讨喜 2020-11-29 16:31

I would like to reproduce the plot immediately below by using ggplot2. I can come close, but cannot remove the top and right borders. Below I present several attempts usin

相关标签:
7条回答
  • 2020-11-29 17:01

    Recent updates to ggplot (0.9.2+) have overhauled the syntax for themes. Most notably, opts() is now deprecated, having been replaced by theme(). Sandy's answer will still (as of Jan '12) generates a chart, but causes R to throw a bunch of warnings.

    Here's updated code reflecting current ggplot syntax:

    library(ggplot2)
    a <- seq(1,20)
    b <- a^0.25
    df <- as.data.frame(cbind(a,b))
    
    #base ggplot object
    p <- ggplot(df, aes(x = a, y = b))
    
    p +
      #plots the points
      geom_point() +
    
      #theme with white background
      theme_bw() +
    
      #eliminates background, gridlines, and chart border
      theme(
        plot.background = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank()
      ) +
    
      #draws x and y axis line
      theme(axis.line = element_line(color = 'black'))
    

    generates:

    plot output

    0 讨论(0)
提交回复
热议问题