多个不同大小的 ggplot

2024-01-05

使用起来比较简单grid.arrange in the gridExtra包来排列矩阵中的多个图,但是如何排列图(我正在处理的图来自ggplot2)当某些地块打算比其他地块更大时?在基地,我可以使用layout()例如下面的例子:

 nf <- layout(matrix(c(1,1,1,2,3,1,1,1,4,5,6,7,8,9,9), byrow=TRUE, nrow=3))
 layout.show(nf)

相当于什么ggplot plots?

一些包含的图

library(ggplot2)
p1 <- qplot(x=wt,y=mpg,geom="point",main="Scatterplot of wt vs. mpg", data=mtcars)
p2 <- qplot(x=wt,y=disp,geom="point",main="Scatterplot of wt vs disp", data=mtcars)
p3 <- qplot(wt,data=mtcars)
p4 <- qplot(wt,mpg,data=mtcars,geom="boxplot")
p5 <- qplot(wt,data=mtcars)
p6 <- qplot(mpg,data=mtcars)
p7 <- qplot(disp,data=mtcars)
p8 <- qplot(disp, y=..density.., geom="density", data=mtcars)
p9 <- qplot(mpg, y=..density.., geom="density", data=mtcars)

您可以使用嵌套arrangeGrob像这样的例子调用:

library(ggplot2)
library(gridExtra)

p <- ggplot(data.frame(x=1, y=1), aes(x,y)) + geom_point()

grid.arrange(
  arrangeGrob(
    p, 
    arrangeGrob(p, p, nrow=2),
    ncol=2 ,widths=c(2,1)),
  arrangeGrob(p, p ,p ,ncol=3, widths=rep(1,3)),
  nrow=2)

Edit:

gl <- lapply(1:9, function(ii) grobTree(rectGrob(),textGrob(ii)))

grid.arrange(
  arrangeGrob(gl[[1]],
              do.call(arrangeGrob, c(gl[2:5], ncol=2)),
              nrow=1,
              widths=3:2),
  do.call(arrangeGrob, c(gl[6:9], nrow=1, list(widths=c(1,1,1,2)))),
nrow=2, heights=c(2,1))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

多个不同大小的 ggplot 的相关文章

随机推荐