56

By default, it seems that ggplot2 uses a minor grid that is just half of the major grid. Is there any way to to break this up?

For example, I have a plot where the x-axis is years, and the major breaks are (1850, 1900, 1950, 2000). This means the minor grid points are at (1875, 1925, 1975), which is a little unintuitive for years. How can I make the minor grid appear at every decade?

naught101
  • 18,687
  • 19
  • 90
  • 138

1 Answers1

96

You do it by explicitly specifying minor_breaks() in the scale_x_continuous. Note that since I did not specify panel.grid.major in my trivial example below, the two plots below don't have those (but you should add those in if you need them). To solve your issue, you should specify the years either as a sequence or just a vector of years as the argument for minor_breaks().

e.g.

 ggplot(movies, aes(x=rating)) + geom_histogram() + 
 theme(panel.grid.minor = element_line(colour="blue", size=0.5)) + 
 scale_x_continuous(minor_breaks = seq(1, 10, 1))

enter image description here

 ggplot(movies, aes(x=rating)) + geom_histogram() + 
 theme(panel.grid.minor = element_line(colour="blue", size=0.5)) + 
 scale_x_continuous(minor_breaks = seq(1, 10, 0.5))

enter image description here

Maiasaura
  • 32,226
  • 27
  • 104
  • 108
  • 6
    With date objects, it appears that this will work: ``scale_x_date(labels = date_format("%Y"), breaks = seq(as.Date("1990-01-01"), as.Date("2016-01-01"), by = "5 years"), minor_breaks = "1 year") `` but ``minor_breaks`` didn't work for me when I set the start and end dates inside a ``seq`` command in a manor anologous to that done for ``breaks``. – PatrickT Oct 17 '15 at 14:20