2013-03-19 26 views
7

這是上一個問題的編輯版本。幾種分佈的成對比較

我們給予通過表正ñ意見(樣本)在變量(基因等),我們期待學習每對觀測的變量的行爲 - 例如,兩個觀測具有最高的正相關或負相關。爲此,我在Stadler et.al中看到了一個很棒的圖表。性質紙(2011):

enter image description here

這可能是所使用的樣品數據集。

m <- 1000 
samples <- data.frame(unif1 = runif(m), unif2 = runif(m, 1, 2), norm1 = rnorm(m), 
         norm2 = rnorm(m, 1), norm3 = rnorm(m, 0, 5)) 

我已經測試包gpairs產生這一個gpairs(samples)。這是一個良好的開端,但別無選擇,把相關係數在右上部分,也沒有對下角的密度圖:

enter image description here

接下來,我用包GGallyggparis(samples, lower=list(continuous="density"))(感謝@LucianoSelzer爲下面的評論)。現在我們在上角和下角的密度之間有相關性,但是我們缺少對角線的條形圖,並且密度圖不是熱圖形。

enter image description here

任何想法,使更接近所需要的圖像(第一個)?

回答

9

您可以嘗試合併幾種不同的繪圖方法併合並結果。下面是一個示例,可以相應調整:

cors<-round(cor(samples),2) #correlations 

# make layout for plot layout 
laymat<-diag(1:5) #histograms 
laymat[upper.tri(laymat)]<-6:15 #correlations 
laymat[lower.tri(laymat)]<-16:25 #heatmaps 

layout(laymat) #define layout using laymat 

par(mar=c(2,2,2,2)) #define marginals etc. 

# Draw histograms, tweak arguments of hist to make nicer figures 
for(i in 1:5) 
    hist(samples[,i],main=names(samples)[i]) 

# Write correlations to upper diagonal part of the graph 
# Again, tweak accordingly 
for(i in 1:4) 
    for(j in (i+1):5){ 
    plot(-1:1,-1:1, type = "n",xlab="",ylab="",xaxt="n",yaxt="n") 
    text(x=0,y=0,labels=paste(cors[i,j]),cex=2) 
    } 

# Plot heatmaps, here I use kde2d function for density estimation 
# image function for generating heatmaps 
library(MASS) 
for(i in 2:5) 
    for(j in 1:(i-1)){ 
    k <- kde2d(samples[,i],samples[,j]) 
    image(k,col=heat.colors(1000)) 
    } 

edit:更正了最後一個循環的索引。 pairwise plot

+0

哇!太好了,謝謝。我很好奇,看看是否有任何偉大和短的ggplot2答案。 – Ali 2013-03-19 22:03:40

+0

我敢打賭,我剛開始熟悉ggplot2,所以我決定走老路。 ggplot2使用網格圖形,所以佈局想法在那裏不起作用。但這可能會有所幫助:http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_%28ggplot2%29/ – 2013-03-20 05:18:00