ggplot2:使用grid.arrange()作为do.call()的参数定义绘图布局 [英] ggplot2: Define plot layout with grid.arrange() as argument of do.call()
本文介绍了ggplot2:使用grid.arrange()作为do.call()的参数定义绘图布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
require(ggplot2)
require(gridExtra)
df < - data.frame(value1 = rnorm(200),
value2 = rnorm(200),
value3 = rnorm(200),
value4 = rnorm(200))
p1 < - ggplot(df)+ geom_density(aes(x = value1))
p2 < - ggplot(df)+ geom_density(aes(x )= $ value = 2))
p3 < - ggplot(df)+ geom_density(aes(x = value3))
p4 < - ggplot(df)+ geom_density(aes(x = value4))
grid.arrange(p1,arrangeGrob(p2,p3,p4,ncol = 3),heights = c(2.5 / 4,1.5 / 4),ncol = 1)
但使用函数
myplot< - function i){
pre>
p < - ggplot(df)+ geom_density(aes_string(x = i))
return(p)
}
和
lapply
调用
<$ (c(value1,value2,value3,value4),myplot)
do.call(grid.arrange,c (p))
在这种情况下 grid.arrange
以2乘2的矩阵分布图。但我想获得一个不平衡的布局,与
grid.arrange(p1,arrangeGrob(p2,p3,p4,ncol = 3),heights = c(2.5 / 4,1.5 / 4),ncol = 1)
解决方案
您现在可以做,
grid.arrange(p1,p2,p3,p4, layout_matrix = rbind(c(1,1,1),c(2,3,4)))
I want to obtained an unbalanced grid of plots such as
require(ggplot2)
require(gridExtra)
df <- data.frame(value1 = rnorm(200),
value2 = rnorm(200),
value3 = rnorm(200),
value4 = rnorm(200))
p1 <- ggplot(df) + geom_density(aes(x=value1))
p2 <- ggplot(df) + geom_density(aes(x=value2))
p3 <- ggplot(df) + geom_density(aes(x=value3))
p4 <- ggplot(df) + geom_density(aes(x=value4))
grid.arrange(p1, arrangeGrob(p2,p3,p4, ncol=3), heights=c(2.5/4, 1.5/4), ncol=1)
but using a function
myplot <- function(i){
p <- ggplot(df) + geom_density(aes_string(x=i))
return(p)
}
and an lapply
call
p <- lapply(c("value1","value2","value3","value4"), myplot)
do.call(grid.arrange, c(p))
In this case grid.arrange
distribute the plots in a 2 by 2 matrix. But I want to obtain an unbalanced layout as with
grid.arrange(p1, arrangeGrob(p2,p3,p4, ncol=3), heights=c(2.5/4, 1.5/4), ncol=1)
解决方案
You can now do,
grid.arrange(p1,p2,p3,p4, layout_matrix = rbind(c(1,1,1),c(2,3,4)))
这篇关于ggplot2:使用grid.arrange()作为do.call()的参数定义绘图布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文