使用rangingGrob在堆叠图中均衡ggplot2面板的高度 [英] equalizing ggplot2 panel heights in a stacked plot with arrangeGrob

查看:64
本文介绍了使用rangingGrob在堆叠图中均衡ggplot2面板的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两组时间序列数据,它们想以堆叠的方式绘制.到目前为止,我已经能够提出这样的建议:

I've got two sets of time-series data that I want to plot in a stacked arrangement. So far I've been able to come up with something like this:

library(ggplot2);
library(gridExtra);

t=1:100; s=sin(t/10); c=cos(t/10);
g1=ggplot()+theme_bw()+geom_line(aes(x=t,y=s))+ylab(NULL)
g2=ggplot()+theme_bw()+geom_line(aes(x=t,y=c))+ylab("Cosine")+xlab("Time")

# get rid of the top plot's axis labels
g1=g1+theme(
  axis.text.x=element_blank(),
  panel.margin = unit(0,"null")
  );
g1=g1+labs(x=NULL);

# zero bottom margin of top plot
g1$theme$plot.margin[3]=unit(0,"null");

# zero top margin of bottom plot
g2$theme$plot.margin[1]=unit(0,"null");

# this trick equalizes the width of the two plot panels
g1g=ggplotGrob(g1);
g2g=ggplotGrob(g2);
g1g$widths=g2g$widths

# however, equalizing the heights of the panels is not so simple as
# the following:
# g1g$heights=g2g$heights

g=arrangeGrob(g1g,g2g)

plot(g)   #ggsave("out.svg",g,width=5,height=1.5);

组合图如下所示.我把它做得特别宽简短,以便您可以看到问题: arrangeGrob 等于绘图高度,但这样做会使绘图 panels 具有不同的高度高度.底部面板被 x 轴标签压扁并打勾底部图上的标签,顶部图没有.

The combined plot is shown below. I made it particularly wide and short so that you could see the problem: arrangeGrob equalizes the plot heights, but doing so makes the plot panels have different heights. The bottom panel gets squished by the x axis label and tick labels on the bottom plot, which the top plot lacks.

现在,我可以回收用来均衡宽度的技巧了.取消注释行

Now, I could recycle the trick I used to equalize the widths. Uncommenting the line

g1g$heights=g2g$heights

产生以下结果:

这不是我想要的,因为现在情节之间出现了过多的垂直空间-我本来希望他们能够触摸.

This is not what I want because of the excessive vertical space which now appears between the plots - I had wanted them to be touching.

我知道我可以将 heights 参数传递给rangingGrob,以指定绘图的相对高度:

I know that I could pass a heights argument to arrangeGrob, to specify the relative heights of the plots:

g=arrangeGrob(g1g,g2g,heights=c(1,2))

但是然后我不得不弄弄数字,直到看起来正确为止.

But then I have to fiddle with the numbers until it looks right.

我想知道是否存在一种简单的方法,可以在渲染最终的grob时自动强制两个面板具有相同的高度.

I'm wondering if there's an easy way to automatically force just the two panels to have the same height when the final grob is rendered.

推荐答案

使用rbind代替,

grid.draw(rbind(ggplotGrob(g1), ggplotGrob(g2)))

,如果您想摆脱它们之间的空间,那么从gtable中删除这些行要比弄乱绘图边距更容易(您对主题设置的手动更改产生了错误,因此我忽略了这些行)

and if you want to get rid off the space in-between, it's easier to remove those rows from the gtable than to mess with the plot margins (your manual changes to the theme settings produced an error so I ignored those lines).

grid.newpage()
grid.draw(rbind(ggplotGrob(g1)[-(4:6),], ggplotGrob(g2)[-(1:2),]))

更改面板高度必须在单独的步骤中完成,例如使用这个小助手功能

Changing the panel heights has to be done in a separate step, e.g. using this little helper function

g12 <- rbind(ggplotGrob(g1)[-(4:6),], ggplotGrob(g2)[-(1:2),])

resize_heights <- function(g, heights = rep(1, length(idpanels))){
  idpanels <- unique(g$layout[grepl("panel",g$layout$name), "t"])
  g$heights <- grid:::unit.list(g$heights)
  g$heights[idpanels] <- unit.c(do.call(unit, list(heights, 'null')))
  g
}

grid.newpage()
grid.draw(resize_heights(g12, c(3,1)))

这篇关于使用rangingGrob在堆叠图中均衡ggplot2面板的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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