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

RGgplot2

R Problem Overview


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!

R Solutions


Solution 1 - R

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.

Solution 2 - R

Try using

> scale_x_continuous(breaks = NULL)

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

Solution 3 - R

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()
      )

Solution 4 - R

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())

Solution 5 - R

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTarekView Question on Stackoverflow
Solution 1 - Rdave.ponetView Answer on Stackoverflow
Solution 2 - RlearnrView Answer on Stackoverflow
Solution 3 - RKaragoldView Answer on Stackoverflow
Solution 4 - RM0MView Answer on Stackoverflow
Solution 5 - RSHKTView Answer on Stackoverflow