我试图将两个用ggplot制作的情节放在相同的页面上,顶部和底部,这样它们的宽度是相同的。数据来自同一时间序列,x轴是时间,因此,重要的是,同一时间的数据点不能相对于彼此水平移动。我尝试了来自package gridExtra的gridExtra:
grid.arrange(p1, p2)但由于y轴标号宽度的不同,图的宽度也不同。我看了处理类似问题的this post,但是我无法应用这些信息来解决我的问题。

发布于 2013-01-30 01:43:18
根据我的评论(以及@Justin答案中间的评论),facet_wrap可能是最好的选择。它生成类似于下面的图像。显然,你需要玩弄色彩,传奇,也许因素的顺序,但你可以看到一般的方法。代码跟随图像。

library(ggplot2)
library(reshape)
mydf <- data.frame(day = 1:10,
upper1 = runif(10, 10000, 20000),
upper2 = runif(10, 15000, 16000),
lower1 = runif(10, 1, 10),
lower2 = runif(10, 3, 8))
mydf.melt <- melt(mydf, id.var = 'day')
mydf.melt$grouping <- ifelse(mydf.melt$value >= 10000, "upper", "lower")
ggplot(mydf.melt, aes(x = day, y = value, group = variable)) +
geom_line() +
facet_wrap(~ grouping, ncol = 1, scales = "free_y")发布于 2013-01-29 19:12:48
grid.arrange的帮助文本说:
Arguments:
...: plots of class ggplot2, trellis, or grobs, and valid
arguments to grid.layout因此,阅读grid.layout会导致:
Arguments:
nrow: An integer describing the number of rows in the layout.
ncol: An integer describing the number of columns in the layout.
widths: A numeric vector or unit object describing the widths of the
columns in the layout.
heights: A numeric vector or unit object describing the heights of the
rows in the layout.换句话说,你可以通过宽度和高度作为向量。grid.arrange(p1, p2, heights=c(1, 2)。或举一个例子:
dat <- data.frame(x=1:10, y=10:1)
q1 <- qplot(x, y, data=dat)
q2 <- qplot(y, x, data=dat)
q3 <- qplot(x, y, data=dat, geom='line')
q4 <- qplot(y, x, data=dat, geom='line')
grid.arrange(q1, q2, q3, q4, heights=1:2, widths=1:2)还值得一提的是,通常可以使用来自reshape2包的reshape2和ggplot2中的facet_wrap或facet_grid来实现类似的效果。
https://stackoverflow.com/questions/14590111
复制相似问题