84

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.

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'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!

Tarek
  • 1,781
  • 2
  • 12
  • 18

5 Answers5

186

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.

dave.ponet
  • 1,981
  • 2
  • 11
  • 5
  • 2
    perhaps this needs an update: it gives me `Error: stat_count() must not be used with a y aesthetic.` – gaspar Jul 11 '19 at 11:45
  • 1
    Error still exists as of 6/19/2020. This solution currently does not work. – Meg Jun 19 '20 at 16:16
  • 1
    @Meg not sure if you are still looking for a solution, but I came across removeGrid from the ggExtra library here: [rdrr.io removeGrid](https://rdrr.io/cran/ggExtra/man/removeGrid.html), which removes the grid lines while retaining the axis labels. – SHKT Jul 17 '20 at 14:42
27

Try using

scale_x_continuous(breaks = NULL)

This would remove all the vertical gridlines as well as x-axis tickmark labels.

PatrickT
  • 8,058
  • 7
  • 59
  • 94
learnr
  • 5,679
  • 4
  • 25
  • 23
  • 9
    That did it, but now, as you noted, I don't have the x-axis labels, which I need - any thoughts as to how to write them back in? – Tarek Apr 21 '10 at 16:49
  • 18
    New version says, `Please use breaks = NULL to remove breaks in the scale. (Deprecated; last used in version 0.8.9) ` – Stat-R Aug 07 '13 at 21:07
  • `scale_x_discrete(breaks = NULL)` is the one to use when the x variable is discrete. – luchonacho Feb 06 '18 at 15:16
  • 4
    This doesn't work when I would like to have labels but no breaks. – groceryheist Feb 04 '20 at 23:58
  • @groceryheist not sure if you are still looking for a solution, but I came across removeGrid from the ggExtra library here: [rdrr.io removeGrid](https://rdrr.io/cran/ggExtra/man/removeGrid.html), which removes the grid lines while retaining the axis labels. – SHKT Jul 17 '20 at 14:38
4

This leaves you only with the data points:

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())
Chase
  • 62,262
  • 16
  • 135
  • 160
M0M
  • 41
  • 1
  • Perhaps this needs to updated. When I run it, I get the following error: Error in opts(panel.background = theme_blank()) : could not find function "opts" Note this answer also does not follow the OP's reprodicible answer, which uses "data" (not "out"), and "x" and "y," not "X1" and "X2." – Meg Jun 19 '20 at 16:14
2

Option 1:

data_df <- data.frame(x = 1:10, y = c(3,5,2,5,6,2,7,6,5,4))

ggplot(data_df, aes(x, y)) +
  geom_bar(stat = 'identity') +
  theme(panel.background = element_rect(fill = "white"))

Option 2:

data_df <- data.frame(x = 1:10, y = c(3,5,2,5,6,2,7,6,5,4))
    
ggplot(data_df, aes(x, y)) +
      geom_bar(stat = 'identity') +
      theme(
        panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank(),
        panel.grid.major.y = element_blank(),
        panel.grid.minor.y = element_blank()
      )
Karagold
  • 21
  • 1
1

Copying my answer from a related thread,

For people looking this up in 2020, I have found a solution in the form of the removeGrid function from the ggExtra library here rdrr.io > removeGrid

I have tested it to be working with ggplot2 version 3.3.0 and ggExtra version 0.9, giving me axis ticks without the gridlines.

SHKT
  • 148
  • 7