如何在 ggplot2 中控制 ylim 以绘制不同比例的多面图?

发布于 2024-09-30 11:13:58 字数 225 浏览 7 评论 0原文

在下面的示例中,如何为每个方面设置单独的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烟织青萝梦 2024-10-07 11:13:58

这是不久前在 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.

水水月牙 2024-10-07 11:13:58

据我所知,这还没有在 ggplot2 中实现。然而,一种解决方法 - 将为您提供超出 ggplot 自动提供的 ylims - 是添加“人工数据”。要减少 ylims,只需删除不需要绘图的数据(参见 和 的示例)。

下面是一个示例:

让我们设置一些要绘制的虚拟数据,

df <- data.frame(x=rep(seq(1,2,.1),4),f1=factor(rep(c("a","b"),each=22)),f2=factor(rep(c("x","y"),22)))
df <- within(df,y <- x^2)

我们可以使用折线图绘制它们

p <- ggplot(df,aes(x,y))+geom_line()+facet_grid(f1~f2,scales="free_y")
print(p)

假设我们想让 y 在第一行中从 -10 开始,在第二行中从 0 开始,所以我们在(0,-10) 到左上图和 (0,0) 到左下图:

ylim <- data.frame(x=rep(0,2),y=c(-10,0),f1=factor(c("a","b")),f2=factor(c("x","y")))
dfy <- rbind(df,ylim)

现在通过将 x 尺度限制在 1 和 2 之间,这些添加的点不会被绘制(给出警告):

p <- ggplot(dfy,aes(x,y))+geom_line()+facet_grid(f1~f2,scales="free_y")+xlim(c(1,2))
print(p)

同样会通过在 xlim 范围之外的 x 值处添加具有较高 y 值的点来扩展上述边距。

如果您想减少 ylim,这将不起作用,在这种情况下,对数据进行子集化将是一个解决方案,例如将上行限制在 -10 和 1.5 之间,您可以使用:

p <- ggplot(dfy,aes(x,y))+geom_line(subset=.(y < 1.5 | f1 != "a"))+facet_grid(f1~f2,scales="free_y")+xlim(c(1,2))
print(p)

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

df <- data.frame(x=rep(seq(1,2,.1),4),f1=factor(rep(c("a","b"),each=22)),f2=factor(rep(c("x","y"),22)))
df <- within(df,y <- x^2)

Which we could plot using line graphs

p <- ggplot(df,aes(x,y))+geom_line()+facet_grid(f1~f2,scales="free_y")
print(p)

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:

ylim <- data.frame(x=rep(0,2),y=c(-10,0),f1=factor(c("a","b")),f2=factor(c("x","y")))
dfy <- rbind(df,ylim)

Now by limiting the x-scale between 1 and 2 those added points are not plotted (a warning is given):

p <- ggplot(dfy,aes(x,y))+geom_line()+facet_grid(f1~f2,scales="free_y")+xlim(c(1,2))
print(p)

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:

p <- ggplot(dfy,aes(x,y))+geom_line(subset=.(y < 1.5 | f1 != "a"))+facet_grid(f1~f2,scales="free_y")+xlim(c(1,2))
print(p)
想你只要分分秒秒 2024-10-07 11:13:58

现在实际上有两个包可以解决这个问题:
https://github.com/zeehio/facetscaleshttps://cran.r-project.org/package=ggh4x
我建议使用 ggh4x,因为它有非常有用的工具,例如多面网格多层(有 2 个定义行或列的变量),根据需要在每个面中缩放 x 和 y 轴,并且还具有多个填充和色标。
对于您的问题,解决方案如下:

library(ggh4x)

scales <- list(
  # Here you have to specify all the scales, one for each facet row in your case
  scale_y_continuous(limits = c(2,10),
  scale_y_continuous(breaks = c(3, 4))
)
qplot(x, value,  data=df, geom=c("smooth")) +
 facet_grid(variable ~ ., scale="free_y") +
 facetted_pos_scales(y = scales)

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:

library(ggh4x)

scales <- list(
  # Here you have to specify all the scales, one for each facet row in your case
  scale_y_continuous(limits = c(2,10),
  scale_y_continuous(breaks = c(3, 4))
)
qplot(x, value,  data=df, geom=c("smooth")) +
 facet_grid(variable ~ ., scale="free_y") +
 facetted_pos_scales(y = scales)
浮世清欢 2024-10-07 11:13:58

我有一个函数 facet_wrap 的示例,

ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  facet_wrap(vars(class), scales = "free", 
             nrow=2,ncol=4)

上面的代码生成的图如下:
我的等级太低,无法上传图片,点击此处查看剧情

I have one example of function facet_wrap

ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  facet_wrap(vars(class), scales = "free", 
             nrow=2,ncol=4)

Above code generates plot as:
my level too low to upload an image, click here to see plot

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文