左alignment两个graphics边(ggplot)

我正在使用ggplot,并有两个图表,我想要显示在彼此之上。 我使用grid.arrange的grid.arrange来堆叠它们。 问题是我希望graphics的左边缘alignment以及右边缘,而不pipe轴标签。 (问题出现是因为一个图的标签很短而另一个图的标签长)。

问题:
我该怎么做? 我不嫁给grid.arrange,但ggplot2是必须的。

我试过了:
我试着用宽度和高度以及ncol和nrow来制作一个2 x 2的网格,并将视觉对象放在对angular上,然后用宽度进行播放,但是我不能在对angular看到视觉效果。

 require(ggplot2);require(gridExtra) A <- ggplot(CO2, aes(x=Plant)) + geom_bar() +coord_flip() B <- ggplot(CO2, aes(x=Type)) + geom_bar() +coord_flip() grid.arrange(A, B, ncol=1) 

在这里输入图像描述

尝试这个,

  gA <- ggplotGrob(A) gB <- ggplotGrob(B) maxWidth = grid::unit.pmax(gA$widths[2:5], gB$widths[2:5]) gA$widths[2:5] <- as.list(maxWidth) gB$widths[2:5] <- as.list(maxWidth) grid.arrange(gA, gB, ncol=1) 

编辑

这是一个更通用的解决scheme(使用任意数量的图)使用rbind.gtable包含的gridExtra的修改版本

 gA <- ggplotGrob(A) gB <- ggplotGrob(B) grid::grid.newpage() grid::grid.draw(rbind(gA, gB)) 

我想把这个推广到任何数量的地块。 这是一个使用Baptiste方法的分步解决scheme:

 plots <- list(A, B, C, D) grobs <- list() widths <- list() 

收集每个阴谋的每个grob的宽度

 for (i in 1:length(plots)){ grobs[[i]] <- ggplotGrob(plots[[i]]) widths[[i]] <- grobs[[i]]$widths[2:5] } 

使用do.call来获得最大宽度

 maxwidth <- do.call(grid::unit.pmax, widths) 

将最大宽度分配给每个grob

 for (i in 1:length(grobs)){ grobs[[i]]$widths[2:5] <- as.list(maxwidth) } 

情节

 do.call("grid.arrange", c(grobs, ncol = 1)) 

使用cowplot包:

 A <- ggplot(CO2, aes(x=Plant)) + geom_bar() +coord_flip() B <- ggplot(CO2, aes(x=Type)) + geom_bar() +coord_flip() library(cowplot) plot_grid(A, B, ncol=1, align="v") 

在这里输入图像描述

http://rpubs.com/MarkusLoew/13295是一个非常简单的解决scheme(最后一项)适用于这个问题:;

 require(ggplot2);require(gridExtra) A <- ggplot(CO2, aes(x=Plant)) + geom_bar() +coord_flip() B <- ggplot(CO2, aes(x=Type)) + geom_bar() +coord_flip() grid.draw(rbind(ggplotGrob(A), ggplotGrob(B), size="first")) 

你也可以使用这个宽度和高度:

 require(ggplot2);require(gridExtra) A <- ggplot(CO2, aes(x=Plant)) + geom_bar() +coord_flip() B <- ggplot(CO2, aes(x=Type)) + geom_bar() +coord_flip() C <- ggplot(CO2, aes(x=conc)) + geom_bar() +coord_flip() D <- ggplot(CO2, aes(x=uptake)) + geom_bar() +coord_flip() grid.draw(cbind( rbind(ggplotGrob(A), ggplotGrob(B), size="first"), rbind(ggplotGrob(C), ggplotGrob(D), size="first"), size='first')) 

这里是另一个可能的解决scheme,使用reshape2包中的meltfacet_wrap

 library(ggplot2) library(reshape2) dat = CO2[, c(1, 2)] dat$id = seq(nrow(dat)) mdat = melt(dat, id.vars="id") head(mdat) # id variable value # 1 1 Plant Qn1 # 2 2 Plant Qn1 # 3 3 Plant Qn1 # 4 4 Plant Qn1 # 5 5 Plant Qn1 # 6 6 Plant Qn1 plot_1 = ggplot(mdat, aes(x=value)) + geom_bar() + coord_flip() + facet_wrap(~ variable, nrow=2, scales="free", drop=TRUE) ggsave(plot=plot_1, filename="plot_1.png", height=4, width=6) 

在这里输入图像描述

egg包将ggplot对象包装成标准化的3x3 gtable,使任意ggplots(包括多面体)之间的绘图面板alignment。

 library(egg) # devtools::install_github('baptiste/egg') library(ggplot2) p1 <- ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) + geom_point() p2 <- ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) + geom_point() + facet_wrap( ~ cyl, ncol=2, scales = "free") + guides(colour="none") + theme() ggarrange(p1, p2) 

在这里输入图像描述

充其量,这是一个黑客:

 library(wq) layOut(list(A, 1, 2:16), list(B, 2:3, 1:16)) 

这感觉真的错了。