How to control number of minor grid lines in ggplot2?

后端 未结 1 539
梦毁少年i
梦毁少年i 2020-12-28 12:45

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

相关标签:
1条回答
  • 2020-12-28 13:24

    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

    0 讨论(0)
提交回复
热议问题