R-在两个ggplotGrob对象上使用rbind时出错

R-在两个ggplotGrob对象上使用rbind时出错,r,ggplot2,R,Ggplot2,我试图遵循这个示例,使用gtable包组合两个图(下面的链接和代码)。我已尝试运行代码以查看输出结果,但在以下行中出现错误: g <- rbind(g1, g2, size="first") # stack the two plots **Error: x and y must have the same number of columns** g在长度不同的g1$widthsundg2$widths中存在问题,因此出现错误。您可以在调用rbind之前运行unit.

我试图遵循这个示例,使用gtable包组合两个图(下面的链接和代码)。我已尝试运行代码以查看输出结果,但在以下行中出现错误:

g <- rbind(g1, g2, size="first") # stack the two plots

**Error: x and y must have the same number of columns**

g在长度不同的
g1$widths
und
g2$widths
中存在问题,因此出现错误。您可以在调用
rbind
之前运行
unit.pmax()。结果是否符合要求

## convert plots to gtable objects
library(gtable)
library(grid) # low-level grid functions are required
g1 <- ggplotGrob(p1)
g1 <- gtable_add_cols(g1, unit(0,"mm")) # add a column for missing legend
g2 <- ggplotGrob(p2)#

# set the same widths for both blots
g1$widths <- unit.pmax(g1$widths, g2$widths)
g2$widths <- unit.pmax(g1$widths, g2$widths)

# stack them afterwards
g <- rbind(g1, g2, size="first") # stack the two plots

g$layout[grepl("guide", g$layout$name),c("t","b")] <- c(1,nrow(g))
grid.newpage()
grid.draw(g)
##将绘图转换为gtable对象
图书馆(gtable)
库(网格)#需要低级网格函数
g1
## convert plots to gtable objects
library(gtable)
library(grid) # low-level grid functions are required
g1 <- ggplotGrob(p1)
g1 <- gtable_add_cols(g1, unit(0,"mm")) # add a column for missing legend
g2 <- ggplotGrob(p2)#

# set the same widths for both blots
g1$widths <- unit.pmax(g1$widths, g2$widths)
g2$widths <- unit.pmax(g1$widths, g2$widths)

# stack them afterwards
g <- rbind(g1, g2, size="first") # stack the two plots

g$layout[grepl("guide", g$layout$name),c("t","b")] <- c(1,nrow(g))
grid.newpage()
grid.draw(g)