How can I suppress the vertical grid lines in the ggplot2 graph?

I am creating a histogram for which the bars are sufficient to show the horizontal (x) placement, so I would like to avoid drawing extra vertical grid lines.

I understand how to style minor and main grids in opts (), but I can’t understand all my life how to suppress only vertical grid lines.

library(ggplot2) data <- data.frame(x = 1:10, y = c(3,5,2,5,6,2,7,6,5,4)) ggplot(data, aes(x, y)) + geom_bar(stat = 'identity') + opts( panel.grid.major = theme_line(size = 0.5, colour = '#1391FF'), panel.grid.minor = theme_line(colour = NA), panel.background = theme_rect(colour = NA), axis.ticks = theme_segment(colour = NA) ) 

At this point, it seems to me that I will need to suppress all the grid lines and then return them back using geom_hline (), which seems kind of a pain (it’s also not entirely clear how I can find the tick / major gridline positions to feed into geom_hline ().)

Any thoughts would be appreciated!

+42
r ggplot2
Apr 20 '10 at 19:47
source share
4 answers

Try using

scale_x_continuous (breaks = NULL)

This will remove all vertical grid lines, as well as tick marks for the x axis.

+10
Apr 21 '10 at 6:55
source share
β€” -

As with ggplot2 0.9.2, it has become easier using "themes". Now you can assign themes separately for panel.grid.major.x and panel.grid.major.y, as shown 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 pretty ugly, but it demonstrates how to remove vertical lines while maintaining horizontal lines and x-axis label labels.

+87
Jan 24 '12 at 18:32
source share
+5
Nov 27 '10 at 10:27
source share

This leaves you with data points only:

 ggplot(out, aes(X1, X2)) + geom_point() + scale_x_continuous(breaks = NULL) + scale_y_continuous(breaks = NULL) + opts(panel.background = theme_blank()) + opts(axis.title.x = theme_blank(), axis.title.y = theme_blank()) 
+4
Mar 10 2018-11-11T00:
source share



All Articles