使用grid.arrange指定图的宽度和高度 [英] Specify widths and heights of plots with grid.arrange
问题描述
我有三块地块,我尝试将它们与grid.arrange结合使用。最后一块地块的高度应该低于前两块地块,所有地块的宽度应该相同。
一个实际的例子:
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))
这里,最后一幅图的宽度比前两幅宽。在我的真实数据中,即使我将文本和标题保留在y轴上,第三个图的宽度仍然不同。
我尝试添加widths:
grid.arrange(arrangeGrob(p1,p2,ncol = 1,nrow = 2),$ b $ (1),高度= c(4,1),宽度= c(2,1))
但它会变成两列图......
p>
我也尝试了另一个代码:
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))
我有相同的宽度,但现在我不知道如何改变高度...
那么,有人能帮我把最终情节的高度和宽度结合起来么? / p>
尝试 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屋!