如何控制gridExtra中的绘图宽度? [英] How to control plot width in gridExtra?

查看:206
本文介绍了如何控制gridExtra中的绘图宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能存在重复:

左对齐两个图形边缘(ggplot)


我试图在同一页面的顶部和底部放置两个使用 ggplot 生成的地块,以使它们的宽度相同。数据来自同一时间序列,x轴是时间,所以重要的是同一时间的数据点不会相对于彼此水平移动。我在gridExtra包中试过 grid.arrange

  grid.arrange p1,p2)

由于y轴标签的宽度不同,图表有不同的宽度。我查看了这篇文章处理类似的问题,但我无法应用这些信息来解决我的问题。

解决方案

在@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)


Possible Duplicate:
left align two graph edges (ggplot)

I am trying to put two plots produced with ggplot on the same page, top and bottom, so that their widths are the same. The data is from the same time series, x axis being time, so it is important that data points with the same time are not shifted horizontally with respect to each other. I tried grid.arrange from package gridExtra:

grid.arrange(p1, p2)

but the plots have different widths due to different widths of y axis labels. I looked at this post that deals with similar problem, but I could not apply that information to solve my problem.

解决方案

As per my comment (and also the comment in the middle of the @Justin answer) facet_wrap may be the way to go. It generates something like the image below. Obviously you'd need to play around with colours, legends and maybe the order of the factors, but you can see the general approach. Code follows image.

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")

这篇关于如何控制gridExtra中的绘图宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆