在下面的(无意义的)示例中,我想同时绘制y1和y2曲线,但根据y1曲线确定ylim,忽略y2。
示例如下:
library(ggplot2)
curves <- data.frame(expand.grid(seq(-2,2,0.1), c(2,4), c(1,2)))
names(curves) <- c("x","p","c")
curves$y1 <- splat(function(x,p,c, ...) c * p * exp(- x^p))(curves)
curves$y2 <- splat(function(x,p,c, ...) c + x * p)(curves)
curves <- melt.data.frame(curves, id.vars=1:3)
ggplot(curves, aes(x, value, color = variable)) +
geom_line() +
facet_grid(p ~ c, scales="free_y")我希望第一行有ylim(0,4),第二行有ylim(0,8)。有什么想法吗?最好是关于如何让ggplot确定正确的限制,而不是手动输入?
发布于 2011-04-17 07:02:59
下面的方法是可行的,尽管它看起来很笨拙。毫无疑问,您可以改进这一点。
我的解决方法是从data.frame中删除您不想包含在绘图中的值:
curves <- subset(curves, !(curves$p==2 & (curves$value>4 | curves$value<0)))
curves <- subset(curves, !(curves$p==4 & (curves$value>8 | curves$value<0)))
ggplot(curves, aes(x, value, color = variable)) +
geom_line() +
facet_grid(p ~ c, scales="free_y")

发布于 2011-04-17 08:31:15
如果您以下面这段冗长的代码结束
ylimits <- c( floor(min(curves$value[curves$variable == "y1"])),
ceiling(max(curves$value[curves$variable == "y1"])) )
ggplot(curves, aes(x, value, color = variable)) +
geom_line() +
facet_grid(p ~ c, scales = "free_y") +
scale_y_continuous(breaks = ylimits[1]:ylimits[2]) +
coord_cartesian(ylim = ylimits) 你听好了,

Y轴缩放基于y1曲线(尽管不是基于4和8)。
https://stackoverflow.com/questions/5689189
复制相似问题