如何在 ggplot2 中控制 ylim 以绘制不同比例的多面图?
在下面的示例中,如何为每个方面设置单独的 ylim?
qplot(x, value, data=df, geom=c("smooth")) + facet_grid(variable ~ ., scale="free_y")
在每个方面,y 轴采用不同的值范围,我希望每个方面都有不同的 ylim。
默认 ylims 对于我想要看到的趋势来说太长了。
In the following example, how do I set separate ylims for each of my facets?
qplot(x, value, data=df, geom=c("smooth")) + facet_grid(variable ~ ., scale="free_y")
In each of the facets, the y-axis takes a different range of values and I would like to different ylims for each of the facets.
The defaults ylims are too long for the trend that I want to see.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是不久前在 ggplot2 邮件列表上提出的。您所要求的目前是不可能的,但我认为它正在进行中。
This was brought up on the ggplot2 mailing list a short while ago. What you are asking for is currently not possible but I think it is in progress.
据我所知,这还没有在 ggplot2 中实现。然而,一种解决方法 - 将为您提供超出 ggplot 自动提供的 ylims - 是添加“人工数据”。要减少 ylims,只需删除不需要绘图的数据(参见 和 的示例)。
下面是一个示例:
让我们设置一些要绘制的虚拟数据,
我们可以使用折线图绘制它们
假设我们想让 y 在第一行中从 -10 开始,在第二行中从 0 开始,所以我们在(0,-10) 到左上图和 (0,0) 到左下图:
现在通过将 x 尺度限制在 1 和 2 之间,这些添加的点不会被绘制(给出警告):
同样会通过在 xlim 范围之外的 x 值处添加具有较高 y 值的点来扩展上述边距。
如果您想减少 ylim,这将不起作用,在这种情况下,对数据进行子集化将是一个解决方案,例如将上行限制在 -10 和 1.5 之间,您可以使用:
As far as I know this has not been implemented in ggplot2, yet. However a workaround - that will give you ylims that exceed what ggplot provides automatically - is to add "artificial data". To reduce the ylims simply remove the data you don't want plot (see at the and for an example).
Here is an example:
Let's just set up some dummy data that you want to plot
Which we could plot using line graphs
Assume we want to let y start at -10 in first row and 0 in the second row, so we add a point at (0,-10) to the upper left plot and at (0,0) ot the lower left plot:
Now by limiting the x-scale between 1 and 2 those added points are not plotted (a warning is given):
Same would work for extending the margin above by adding points with higher y values at x values that lie outside the range of xlim.
This will not work if you want to reduce the ylim, in which case subsetting your data would be a solution, for example to limit the upper row between -10 and 1.5 you could use:
现在实际上有两个包可以解决这个问题:
https://github.com/zeehio/facetscales 和 https://cran.r-project.org/package=ggh4x。
我建议使用 ggh4x,因为它有非常有用的工具,例如多面网格多层(有 2 个定义行或列的变量),根据需要在每个面中缩放 x 和 y 轴,并且还具有多个填充和色标。
对于您的问题,解决方案如下:
There are actually two packages that solve that problem now:
https://github.com/zeehio/facetscales, and https://cran.r-project.org/package=ggh4x.
I would recommend using
ggh4x
because it has very useful tools, such as facet grid multiple layers (having 2 variables defining the rows or columns), scaling the x and y-axis as you wish in each facet, and also having multiple fill and colour scales.For your problems the solution would be like this:
我有一个函数 facet_wrap 的示例,
上面的代码生成的图如下:
我的等级太低,无法上传图片,点击此处查看剧情
I have one example of function facet_wrap
Above code generates plot as:
my level too low to upload an image, click here to see plot