R 如何控制ggplot2中具有不同比例的刻面图的ylim?

R 如何控制ggplot2中具有不同比例的刻面图的ylim?,r,visualization,ggplot2,R,Visualization,Ggplot2,在下面的示例中,如何为每个方面设置单独的ylim qplot(x, value, data=df, geom=c("smooth")) + facet_grid(variable ~ ., scale="free_y") 在每个面中,y轴采用不同的值范围,我想为每个面指定不同的Ylim 默认的ylims对于我想要看到的趋势来说太长了。这是不久前的事了。您的要求目前不可能实现,但我认为正在进行中。据我所知,这尚未在ggplot2中实现。然而,一个解决办法是添加“人工数据”,这将使您的YLIM自

在下面的示例中,如何为每个方面设置单独的ylim

qplot(x, value,  data=df, geom=c("smooth")) + facet_grid(variable ~ ., scale="free_y")
在每个面中,y轴采用不同的值范围,我想为每个面指定不同的Ylim


默认的ylims对于我想要看到的趋势来说太长了。

这是不久前的事了。您的要求目前不可能实现,但我认为正在进行中。

据我所知,这尚未在ggplot2中实现。然而,一个解决办法是添加“人工数据”,这将使您的YLIM自动超出ggplot提供的范围。要减少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)

df现在实际上有两个包可以解决这个问题:
,及。
我建议使用
ggh4x
,因为它有非常有用的工具,例如镶嵌面网格多层(有两个变量定义行或列)、在每个镶嵌面中按您的意愿缩放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)
库(ggh4x)

有什么消息吗?现在可能吗?
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)
p <- ggplot(dfy,aes(x,y))+geom_line()+facet_grid(f1~f2,scales="free_y")+xlim(c(1,2))
print(p)
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)
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)