使用 grid.arrange 指定绘图的宽度和高度 [英] Specify widths and heights of plots with grid.arrange

查看:85
本文介绍了使用 grid.arrange 指定绘图的宽度和高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个图,我尝试将它们与 grid.arrange 结合起来.最后一个图的高度应该小于前两个图的高度,并且所有的图都应该具有相同的宽度.

一个工作示例:

p1 <- qplot(mpg, wt, data=mtcars)p2 <- p1p3 <- p1 + 主题(axis.text.y=element_blank(),axis.title.y=element_blank())grid.arrange(arrangeGrob(p1,p2, ncol=1, nrow=2),排列格罗布(p3,ncol=1,nrow=1),高度=c(4,1))

在这里,最后一个图的宽度比前两个大.在我的真实数据中,即使我将文本和标题保留在 y 轴上,第三个绘图的宽度仍然不同.

我尝试添加宽度":

 grid.arrange(arrangeGrob(p1,p2, ncol=1, nrow=2),排列格罗布(p3,ncol=1,nrow=1),高度=c(4,1),宽度=c(2,1))

但它变成了两列图...

我还尝试了另一个代码:

p1 <- ggplotGrob(p1)p2 <- ggplotGrob(p2)p3 <- ggplotGrob(p3)#stripT <-subset(p2$layout, grepl("spacer", p2$layout$name))p3 <- p3[-stripT$t, ]grid.draw(rbind(p1, p2, p3, size = "first"))

我有相同的宽度,但现在我不知道如何更改高度...

那么,有人可以帮我将高度和宽度方面结合起来制作最终图吗?

解决方案

Try plot_grid 来自

I have three plots and I try to combine them with grid.arrange. The last plot should have a smaller height than the first two plots and all the plots should have the same width.

A working example:

p1 <- qplot(mpg, wt, data=mtcars)
p2 <- p1
p3 <- p1 + theme(axis.text.y=element_blank(), axis.title.y=element_blank())

grid.arrange(arrangeGrob(p1,p2, ncol=1, nrow=2),
         arrangeGrob(p3, ncol=1, nrow=1), heights=c(4,1)) 

Here, the last plot has a larger width than the first two. In my real data, even if I keep the text and the title on the y-axis, I still have a different width for the third plot.

I tried to add "widths":

 grid.arrange(arrangeGrob(p1,p2, ncol=1, nrow=2),
         arrangeGrob(p3, ncol=1, nrow=1), heights=c(4,1), widths=c(2,1))

But it turns into a two column plot...

I also tried another code:

p1 <- ggplotGrob(p1)
p2 <- ggplotGrob(p2)
p3 <- ggplotGrob(p3)
# 
stripT <- subset(p2$layout, grepl("spacer", p2$layout$name))
p3 <- p3[-stripT$t, ]

grid.draw(rbind(p1, p2, p3, size = "first")) 

I have the same widths but now I don't know how to change the heights...

Well, can someone help me to combine both the height and width aspects for a final plot?

解决方案

Try plot_grid from the cowplot package:

library(ggplot2)
library(gridExtra)
library(cowplot)
p1 <- qplot(mpg, wt, data=mtcars)
p2 <- p1
p3 <- p1 + theme(axis.text.y=element_blank(), axis.title.y=element_blank())
plot_grid(p1, p2, p3, align = "v", nrow = 3, rel_heights = c(1/4, 1/4, 1/2))

这篇关于使用 grid.arrange 指定绘图的宽度和高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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