首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在gridExtra中控制地块宽度?

如何在gridExtra中控制地块宽度?
EN

Stack Overflow用户
提问于 2013-01-29 18:51:54
回答 2查看 27.8K关注 0票数 9

可能重复: left align two graph edges (ggplot)

我试图将两个用ggplot制作的情节放在相同的页面上,顶部和底部,这样它们的宽度是相同的。数据来自同一时间序列,x轴是时间,因此,重要的是,同一时间的数据点不能相对于彼此水平移动。我尝试了来自package gridExtra的gridExtra:

代码语言:javascript
复制
grid.arrange(p1, p2)

但由于y轴标号宽度的不同,图的宽度也不同。我看了处理类似问题的this post,但是我无法应用这些信息来解决我的问题。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-01-30 01:43:18

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

代码语言:javascript
复制
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")
票数 1
EN

Stack Overflow用户

发布于 2013-01-29 19:12:48

grid.arrange的帮助文本说:

代码语言:javascript
复制
Arguments:

     ...: plots of class ggplot2, trellis, or grobs, and valid
          arguments to grid.layout

因此,阅读grid.layout会导致:

代码语言:javascript
复制
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)。或举一个例子:

代码语言:javascript
复制
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包的reshape2ggplot2中的facet_wrapfacet_grid来实现类似的效果。

票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14590111

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档