我在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