你如何为不同的方面设置不同的比例限制? [英] How do you set different scale limits for different facets?

查看:91
本文介绍了你如何为不同的方面设置不同的比例限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一些示例数据:

  dfr < -  data.frame(
x = rep.int(1: )
y = runif(20),
g = factor(rep(字母[1:2],each = 10))



  

> p <-ggplot(dfr,aes(x,y))+
geom_point()+
facet_wrap(〜g,scales =free_y)

我可以使用

 <$ c设置所有面板的轴限制$ c> p + scale_y_continuous(limits = c(0.2,0.8))

这就像 ylim



但是如何为不同的面设置不同的轴限制?



这样做的方式是将一个列表传递给这个参数,例如,

  p + scale_y_continuous(limits = list(c(0.2,0.8),c(0,0.5)))

不幸的是,这只是在ggplot2案例中抛出了一个错误。



编辑:



这是一个部分破解。如果你想扩展缩放的范围,那么你可以在你的数据集中添加指定限制的列,然后用 geom_blank 来绘制它们。



已修改的数据集:

  dfr < -  data.frame(
x = rep.int( ),
y = runif(20),
g = factor(rep(字母[1:2],each = 10)),
ymin = rep(c( - 0.6,0.3),每个= 10),
ymax = rep(c(1.8,0.5),each = 10)



更新图:

  p + geom_blank(aes(y = ymin) )+ geom_blank(aes(y = ymax))

现在尺度是不同的,是正确的。不幸的是,右手比例并没有收缩,因为它需要为积分腾出空间。



如果有帮助,我们现在可以将问题改为是不是可以在没有重新计算比例的情况下绘制点并且不显式调用 scale_y_continuous

解决方案

我认为这在ggplot2中是不可能的。此来自1月份的讨论表明这个问题正在考虑之中。

Some sample data:

dfr <- data.frame(
  x = rep.int(1:10, 2),
  y = runif(20),
  g = factor(rep(letters[1:2], each = 10))
)

A simple scatterplot with two facets:

p <- ggplot(dfr, aes(x, y)) + 
  geom_point() +
  facet_wrap(~ g, scales = "free_y")

I can set the axis limits for all panels with

p + scale_y_continuous(limits = c(0.2, 0.8))

(or a wrapper for this like ylim)

but how do I set different axis limits for different facets?

The latticey way to do it would be to pass a list to this argument, e.g.,

p + scale_y_continuous(limits = list(c(0.2, 0.8), c(0, 0.5)))

Unfortunately that just throws an error in the ggplot2 case.

EDIT:

Here's a partial hack. If you want to extend the range of the scales then you can add columns to your dataset specifying the limits, then draw them with geom_blank.

Modified dataset:

dfr <- data.frame(
  x = rep.int(1:10, 2),
  y = runif(20),
  g = factor(rep(letters[1:2], each = 10)),
  ymin = rep(c(-0.6, 0.3), each = 10),
  ymax = rep(c(1.8, 0.5), each = 10)
)

Updated plot:

p + geom_blank(aes(y = ymin)) + geom_blank(aes(y = ymax))

Now the scales are different and the left hand one is correct. Unfortunately, the right hand scale doesn't contract since it needs to make room for the points.

In case it helps, we can now rephrase the question as "is it possible to draw points without the scales being recalculated and without explicitly calling scale_y_continuous?"

解决方案

I don't think this is possible yet in ggplot2. This discussion from January suggests the issue is under consideration.

这篇关于你如何为不同的方面设置不同的比例限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆