本文介绍了如何控制ggplot2中次网格线的数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,ggplot2似乎使用只占主要网格一半的次要网格。有什么办法可以解决这个问题吗?



例如,我有一个情节,其中x轴是几年,主要的休息时间是(1850年,1900年, 1950年,2000年)。这意味着小格点在(1875年,1925年,1975年),这是多年来有点不直观。我怎样才能使小网格出现在每一个十年?

你可以通过显式指定 minor_breaks () scale_x_continuous 中。请注意,因为我在下面的例子中没有指定 panel.grid.major ,所以下面的两个图没有这些(但是如果你需要的话,你应该添加这些图)他们)。为了解决你的问题,你应该指定年份作为序列或者仅仅是一个年份向量作为 minor_breaks()的参数。



例如

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

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?

解决方案

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

这篇关于如何控制ggplot2中次网格线的数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 20:26