2013-03-19 80 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