r - 使用 gtable 对象进行排列

标签 r ggplot2 gridextra

来自 https://stackoverflow.com/a/13295880我学会了如何用对齐的绘图区域排列两个绘图。

我的问题是:我怎样才能得到一个排列好的图的对象?

例子:

require(ggplot2)
require(gridExtra)

A <- ggplot(CO2, aes(x=Plant)) + geom_bar() +coord_flip() 
B <- ggplot(CO2, aes(x=Type)) + geom_bar() +coord_flip() 

gA <- ggplot_gtable(ggplot_build(A))
gB <- ggplot_gtable(ggplot_build(B))
maxWidth = grid::unit.pmax(gA$widths[2:3], gB$widths[2:3])
gA$widths[2:3] <- as.list(maxWidth)
gB$widths[2:3] <- as.list(maxWidth)

## works:
grid.arrange(gA, gB, ncol=1)

## does not work:
theplot <- grid.arrange(gA, gB, ncol=1, plot=FALSE)

最佳答案

使用功能 arrangeGrob()将两个图都保存为对象。

theplot <- arrangeGrob(gA, gB, ncol=1)

关于r - 使用 gtable 对象进行排列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15942313/

相关文章:

r - ggplot 中三个时间点的箱线图

r - 在 ggplot2 中绘制每个级别的平均值

r - 使用 grid.arrange 截断标题

r - 对齐 ggplot choropleth 的边缘(图例标题各不相同)

r - 在 LESS TIME 中将秒间隔数据转换为每小时平均表示

r - 在 RMarkdown Slidy Presentations 中自定义 CSS

r - 之前在 Shiny 中输入的内容

R - ggplot2 - 使用栅格作为灰度 basemap

r - 在 r 中的 facet ggplot 的每个面板上添加子表

r - 为什么列表标签按字面意思使用标签名称,如何防止这种情况发生?