Explore Courses Blog Tutorials Interview Questions
0 votes
in R Programming by (5.3k points)

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 understand how to style the minor and major gridlines in opts(), but I can't for the life of me figure out how to suppress just the vertical gridlines.


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') +


    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's looking like I'm going to have to suppress all of the gridlines and then draw them back in with geom_hline(), which seems like kind of a pain (also, it's not entirely clear how I can find the tick/major gridline positions to feed to geom_hline().)

Any thoughts would be appreciated!

1 Answer

0 votes

To suppress the vertical gridlines in ggplot2, you can add the following code to your plot:

scale_x_continuous(breaks = NULL)

In your case:

ggplot(data, aes(x, y)) +

  geom_bar(stat = 'identity') +


    panel.grid.major = element_line(size = 0.5, colour = '#1391FF'),

    panel.grid.minor = element_line(colour = NA),

    panel.background = element_rect(colour = NA),

    axis.ticks = element_line(colour = NA)


  scale_x_continuous(breaks = NULL)


Related questions

Browse Categories