如何获得“不平衡”的 gggplot 网格?

使用 grid.arrange,我可以在一个网格中排列多个 ggplot图形,通过使用如下方法实现多面板图形:

library(ggplot2)
library(grid)
library(gridExtra)

然后生成一些 ggplot2图

plot5 <- grid.arrange(plot4, plot1, heights=c(3/4, 1/4), ncol=1, nrow=2)

我怎样才能获得一个“不平衡的”2个地块布局在整个第一个地块和三个地块在第二个地块? 我尝试了一种“网格的网格”方法,试图使用 grid.arrange来绘制一个网格(如上面的 plot5)与另一个网格的对比图,但是得到了:

安排 Grob 中的错误(... ,as.table = as.table,clip = clip,main = main,: 输入必须是掠夺!

更新:

谢谢你的建议。我将查看 viewportsgrid。同时,多亏了@DWin,‘ wq’包中的 layOut函数对于我的 Sweave文档中的编译图非常有效: enter image description here

更新2:

arrangeGrob命令(如@Baptiste 建议的那样)也可以很好地工作,而且看起来非常直观——至少可以很容易地改变两列的宽度。它还具有不需要“ wq”包的好处。

下面是我的 Sweave 文件中的代码:

<<label=fig5plot, echo=F, results=hide>>=
plot5<-grid.arrange(plot4, arrangeGrob(plot1, plot2, plot3, ncol=1),
ncol=2, widths=c(1,1.2))
@
\begin{figure}[]
\begin{center}
<<label=fig5,fig=TRUE,echo=T, width=10,height=12>>=
<<fig5plot>>
@
\end{center}
\caption{Combined plots using the `arrangeGrob' command.}
\label{fig:five}
\end{figure}

其产出如下: enter image description here

顺便问一下,有人告诉我为什么“ NA”会出现吗?

35557 次浏览

I tried figuring it out with grid and thought I had it down but ended up failing (although looking now at the code in the function I cite below, I can see that I was really close ... :-)

The 'wq' package has a layOut function that will do it for you:

p1 <- qplot(mpg, wt, data=mtcars)
layOut(list(p1, 1:3, 1),   # takes three rows and the first column
list(p1, 1, 2),    # next three are on separate rows
list(p1, 2,2),
list(p1, 3,2))

enter image description here

grid.arrange draws directly on the device; if you want to combine it with other grid objects you need arrangeGrob, as in

 p = rectGrob()
grid.arrange(p, arrangeGrob(p,p,p, heights=c(3/4, 1/4, 1/4), ncol=1),
ncol=2)

Edit (07/2015): with v>2.0.0 you can use the layout_matrix argument,

 grid.arrange(p,p,p,p, layout_matrix = cbind(c(1,1,1), c(2,3,4)))

Another alternative is the patchwork package by Thomas Lin Pedersen.

# install.packages("devtools")
# devtools::install_github("thomasp85/patchwork")
library(patchwork)

Generate some plots.

p1 <- ggplot(mtcars) + geom_point(aes(mpg, disp)) + facet_grid(rows = vars(gear))
p2 <- ggplot(mtcars) + geom_boxplot(aes(gear, disp, group = gear))
p3 <- ggplot(mtcars) + geom_smooth(aes(disp, qsec))
p4 <- ggplot(mtcars) + geom_bar(aes(carb))

Now arrange the plots.

p1 + (p2 / p3 / p4)

enter image description here

There is also multipanelfigure package that is worth to mention. See also this answer.

library(ggplot2)
theme_set(theme_bw())


q1 <- ggplot(mtcars) + geom_point(aes(mpg, disp))
q2 <- ggplot(mtcars) + geom_boxplot(aes(gear, disp, group = gear))
q3 <- ggplot(mtcars) + geom_smooth(aes(disp, qsec))
q4 <- ggplot(mtcars) + geom_bar(aes(carb))


library(magrittr)
library(multipanelfigure)


figure1 <- multi_panel_figure(columns = 2, rows = 3, panel_label_type = "upper-roman")


figure1 %<>%
fill_panel(q1, column = 1, row = 1:3) %<>%
fill_panel(q2, column = 2, row = 1) %<>%
fill_panel(q3, column = 2, row = 2) %<>%
fill_panel(q4, column = 2, row = 3)
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'
figure1

Created on 2018-07-16 by the reprex package (v0.2.0.9000).

Another option is using the plot_grid function from the cowplot package. To create nested grid plots, you can combine multiple plot_grid to show multiple plots with different grids. Here is a reproducible example:

library(ggplot2)
library(cowplot)


# plots
p1 <- ggplot(mtcars, aes(disp, mpg)) +
geom_point()
p2 <- ggplot(mtcars, aes(carb)) +
geom_bar()
p3 <- ggplot(mtcars, aes(x = NULL, y = disp)) +
geom_boxplot()


# Left column with two plots
left_col <- plot_grid(p1, p2, ncol = 1)
# Combine leff_col with third plot
plot_grid(left_col, p3, ncol = 2)

Created on 2022-08-22 with reprex v2.0.2

As you can see in the example, plot 1 and 2 are from one grid combined with p3 which give the current result. You can modify this to whatever you want.