使用gtable排列ggplot图(具有相同宽度的grob)以创建2x2布局 [英] Arrange ggplot plots (grobs with same widths) using gtable to create 2x2 layout
问题描述
我试图使用grobs和gtable将4个(ggplot2)图放置到2x2网格中。我不知道如何设置宽度,也不是1xn或nx1的排列方式。
$ b data(iris)
a < - ggplot(iris,aes(x = Species,y = Petal.Width))+ geom_boxplot(color =black )+ ylab(表达式(Foo_Bar_(g_cm ^ { - 3})))
b <-ggplot(虹膜,aes(x =物种,y = Petal.Length * 100))+ geom_boxplot (color =black)+ ylab(foobar(mm))
c < - ggplot(iris,aes(x = Species,y = Sepal.Width))+ geom_boxplot(color =black)) + ylab(foobar(%))
d < - ggplot(iris,aes(x = Species,y = log10(Sepal.Length)))+ geom_boxplot(color =black)+ ylab( foobar(cm))
plot < - list(a,b,c,d)
grobs = lapply(plots,ggplotGrob)
g = do.call(rbind ,c(grobs,size =first))
g $ widths = do.call(unit.pmax,lapply(grobs,[[,widths))
grid .newpage()
grid.draw(g)
我可以创建以下1x4排列。
如果我使用grid.arrange作为两列,在4个地块上,地块是不同的宽度。
我怎样才能将情节绑定到一个4×4安排的gtable?
#我想也许我可以绑定,然后绑定,但这不起作用
plots1< - list(a,b)
plots2< - list(c,d)
grobs1 = lapply(plots1,ggplotGrob)
grobs2 (cbind,c(grobs1,size =first))
g2 = do.call(cbind,c(grobs2,如果你不知道它是什么,那么你可以使用它来创建你自己的应用程序。 / pre>
解决方我认为你已经有了答案。
你的最后一行返回一个错误,但是一个小的编辑会产生一个组合图,其中列内的宽度相同:
g3 = do.call(rbind,c(list(g1,g2),size =first))#combine g1 and g2 into a list
美学/参考文章:
如果您的x轴是相同的,你可以从最上面的两个图中删除它。
library(ggplot2);库(gridExtra);库(网格)
#调整边距以用尽空白。边距:顶部,右侧,底部,左侧
a1< - a +主题(axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis .ticks.x = element_blank(),
plot.margin = unit(c(1,1,-0.5,0.5),lines))
b1 < - b + theme(axis。 title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
plot.margin = unit(c(1, 1,-0.5,0.5),lines))
c1 < - c + theme(plot.margin = unit(c(0,1,0.5,0.5),lines))$ b $ (a1,b1,b1,b2,...,d1) - 主题(plot.margin =单位(c(0,1,0.5,0.5),lines))
grobz < - lapply (grobz [[1]],grobz [[3]],大小=最后),$ b $(例如,c1,d1),ggplotGrob)
grobz.plot< - arrangeGrob bbbind(grobz [[2]],grobz [[4]],size =last)),
ncol = 2)
grid.draw(grobz.plot)
这些StackOverflow问题有助于对齐图表:
- 在gtable中使用
rbind
设置绘图宽度(Baptiste)[]
- gtable(Baptiste)中的相对面板高度[链接]
- 绘制宽度和图例[ link ]
I am attempting to use grobs and gtable to arrange 4 (ggplot2) plots into a 2x2 grid. I don't know how to set widths, and also a non- 1xn, or nx1 arrangement.
Using this code:
data(iris)
a <- ggplot(iris, aes(x=Species, y=Petal.Width)) + geom_boxplot(color="black") + ylab(expression(Foo~Bar~(g~cm^{-3})))
b <- ggplot(iris, aes(x=Species, y=Petal.Length*100)) + geom_boxplot(color="black") + ylab("foobar (mm)")
c <- ggplot(iris, aes(x=Species, y=Sepal.Width)) + geom_boxplot(color="black") + ylab("foobar (%)")
d <- ggplot(iris, aes(x=Species, y=log10(Sepal.Length))) + geom_boxplot(color="black") + ylab("foobar (cm)")
plots <- list(a,b,c,d)
grobs = lapply(plots, ggplotGrob)
g = do.call(rbind, c(grobs, size="first"))
g$widths = do.call(unit.pmax, lapply(grobs, "[[", "widths"))
grid.newpage()
grid.draw(g)
I can create the following 1x4 arrangement.
If I use grid.arrange for two columns, on the 4 plots, the plots are of different widths.
How can I bind plots into a gtable for a 4 x 4 arrangement?
# I thought maybe I could cbind, then rbind, but this does not work
plots1 <- list(a,b)
plots2 <- list(c,d)
grobs1 = lapply(plots1, ggplotGrob)
grobs2 = lapply(plots2, ggplotGrob)
g1 = do.call(cbind, c(grobs1, size="first"))
g2 = do.call(cbind, c(grobs2, size="first"))
# g3 = do.call(rbind, c(g1,g2, size="first")) #this does not work
解决方案 I think you already had the answer.
Your last line returns an error, but a small edit results in a combined plot where widths within columns are the same:
g3 = do.call(rbind, c(list(g1,g2), size="first")) #combine g1 and g2 into a list
A sidenote for aesthetics/reference:
If your x-axis is the same, you can drop it from the top two plots.
library(ggplot2); library(gridExtra); library(grid)
# Tweak the margins to use up empty space. Margins: Top, Right, Bottom, Left
a1 <- a + theme(axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x= element_blank(),
plot.margin= unit(c(1, 1, -0.5, 0.5), "lines") )
b1 <- b + theme(axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x= element_blank(),
plot.margin= unit(c(1, 1, -0.5, 0.5), "lines") )
c1 <- c + theme(plot.margin= unit(c(0, 1, 0.5, 0.5), "lines") )
d1 <- d + theme(plot.margin= unit(c(0, 1, 0.5, 0.5), "lines") )
grobz <- lapply(list(a1, b1, c1, d1), ggplotGrob)
grobz.plot <- arrangeGrob( grobs = list(rbind(grobz[[1]], grobz[[3]], size = "last"),
rbind(grobz[[2]], grobz[[4]], size = "last")),
ncol = 2)
grid.draw(grobz.plot)
These StackOverflow questions are helpful in aligning plots:
- Using
rbind
in gtable to set plot width (Baptiste) [link]
- Relative panel heights in gtable (Baptiste) [link]
- Plot widths and legends [link]
这篇关于使用gtable排列ggplot图(具有相同宽度的grob)以创建2x2布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!