Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I suppress the vertical gridlines in a ggplot2 plot while retaining the x-axis labels?

Tags:

r

ggplot2

This is a follow-on from this question, in which I was trying to suppress the vertical gridlines.

The solution, as provided by learnr, was to add scale_x_continuous(breaks = NA), but this had the side effect of also suppressing the x-axis labels, as well. I am totally happy to write the labels back in by hand, but it's not clear to me how to figure out where the labels should go.

The other option is to suppress all gridlines (using opts( panel.grid.major = theme_blank()) or some such) and then drawing back in just the major horizontal gridlines. Again, the problem here is how to figure out what the breaks are in the plot to supply to geom_hline().

So, essentially, my options are:

  1. Suppress vertical gridlines and x-axis labels (using scale_x_continuous(breaks = NA) ) and add the x-axis labels back in.
  2. Suppress all gridlines (using opts( panel.grid.major = theme_blank()) ) and add the major horizontal gridlines back in using geom_hline().

Here are the two options:

library(ggplot2)

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

# suppressing vertical gridlines and x-axis labels
# need to re-draw x-axis labels
ggplot(data, aes(x, y)) +
  geom_bar(stat = 'identity') +
  scale_x_continuous(breaks = NA) +
  opts(
    panel.grid.major = theme_line(size = 0.5, colour = '#1391FF'),
    panel.grid.minor = theme_blank(),
    panel.background = theme_blank(),
    axis.ticks = theme_blank()
  )

# suppressing all gridlines
# need to re-draw horizontal gridlines, probably with geom_hbar() 
ggplot(data, aes(x, y)) +
  geom_bar(stat = 'identity') +
  scale_x_continuous(breaks = NA) +
  opts(
    panel.grid.major = theme_blank(),
    panel.grid.minor = theme_blank(),
    panel.background = theme_blank(),
    axis.ticks = theme_blank()
  )
like image 268
Tarek Avatar asked Apr 21 '10 17:04

Tarek


People also ask

How do I get rid of vertical gridlines in ggplot2?

To remove vertical grid lines scale_x_continuous() function is passed with the breaks parameter as NULL.

How do I remove gridlines from a graph in R?

To remove a particular panel grid, use element_blank() for the corresponding theme argument. For example to remove the major grid lines for the x axis, use this: p + theme(panel. grid. major.

How do you add horizontal gridlines in R?

Use the abline() command to add gridlines to R plots.


2 Answers

As code in comments does not display nicely, so I am posting this as an answer. You could do something like this and add labels manually with geom_text():

ggplot(data, aes(x, y)) +
        geom_bar(stat = 'identity') +
        scale_x_continuous(breaks = NA) +
        opts(
                panel.grid.major = theme_line(size = 0.5, colour = '#1391FF'),
                panel.grid.minor = theme_blank(),
                panel.background = theme_blank(),
                axis.ticks = theme_blank()
        )+
        geom_text(aes(label = x, y = -.3))
like image 68
learnr Avatar answered Oct 02 '22 13:10

learnr


The answers above will not work in ggplot2 version 0.9.2.1 and above. Fortunately, there is now an easier way to do this, as described in response to a different question: https://stackoverflow.com/a/8992102/800044.

like image 28
dave.ponet Avatar answered Oct 02 '22 13:10

dave.ponet