我在ggplot中有12个图,并且我正在使用grid.arrange进行排列。我手动设置网格中的行数为4,列数为3.由于3 x 4 = 12,这就像一个魅力。计算R中给定数量的图的最佳网格布局尺寸
但是如果我有任意数量的地块呢?说13 ...我将如何以编程方式查找要使用的行数和列数,使整个绘图成为最“方形”的形状?
我想
更新 链接为此在R.数据:http://github.com/ngfrey/DataGenii/blob/master/exampleMedicalData.csv
这里是我工作的这个早晨的代码。希望它能提供一个更具说明性的例子。请注意我是如何设置的功能return(list(plots=plots, numrow=4, numcol=3))
部分行数和列数:
makePlots<- function(fdf){
idx<- which(sapply(fdf, is.numeric))
idx<- data.frame(idx)
names(idx)<- "idx"
idx$names<- rownames(idx)
plots<- list()
for(i in 2:length(idx$idx)) {
varname<- idx$names[i]
mydata<- fdf[, idx$names[i]]
mydata<- data.frame(mydata)
names(mydata)<- varname
g<- ggplot(data=mydata, aes_string(x=varname))
g<- g + geom_histogram(aes(y=..density..), color="black", fill='skyblue')+ geom_density() + xlab(paste(varname))
print(g)
plots<- c(plots, list(g))
}
return(list(plots=plots, numrow=4, numcol=3))
}
res<- makePlots(fdf)
do.call(grid.arrange, c(res$plots, nrow=res$numrow, ncol=res$numcol))
情节生成和你事先不知道他们有多少呢? – SabDeM
你必须更具体地说明你的意思是方形的,否则'ceiling(sqrt(13))'? –
我每天都会得到多个数据集,每个数据集都有数量未知的数值变量。我想为各个数据集中的每个数值变量创建一个密度图的网格。我会在一分钟内添加一个例子。 – nate