How can I suppress the vertical gridlines in a ggplot2 plot?

前端 未结 5 1111
无人及你
无人及你 2020-12-01 04:14

I am building a bar chart for which bars suffice as indications of horizontal (x) placement, so I\'d like to avoid drawing the superfluous vertical gridlines.

I unde

5条回答
  •  再見小時候
    2020-12-01 04:45

    As of ggplot2 0.9.2, this has become much easier to do using "themes." You can now assign themes separately to panel.grid.major.x and panel.grid.major.y, as demonstrated below.

    #   simulate data for the bar graph
    data <- data.frame( X = c("A","B","C"), Y = c(1:3) )    
    
    #   make the bar graph
    ggplot( data  ) +
        geom_bar( aes( X, Y ) ) +
        theme( # remove the vertical grid lines
               panel.grid.major.x = element_blank() ,
               # explicitly set the horizontal lines (or they will disappear too)
               panel.grid.major.y = element_line( size=.1, color="black" ) 
        )
    

    The result of this example is quite ugly looking, but it demonstrates how to remove the vertical lines while preserving the horizontal lines and x-axis tick-marks.

提交回复
热议问题