Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
414 views
in Technique[技术] by (71.8m points)

r - How to control number of minor grid lines in ggplot2?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...