【问题标题】:Pairwise graphical comparison of several distributions几种分布的成对图形比较
【发布时间】:2013-03-08 13:29:03
【问题描述】:

这是上一个问题的编辑版本。

n 为我们提供了一个 m 表,其中包含 m 个对 m 个变量(基因)的观察(样本)等),并且我们正在研究每对观察值之间的变量行为 - 例如具有最高正相关或负相关性的两个观察值。为此,我在 Stadler 等人中看到了一张很棒的图表。自然论文(2011):

这里可能是要使用的示例数据集。

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(samples) 的包 gpairs 产生这个。这是一个好的开始,但无法将相关系数放在右上角,也无法将密度图放在下角:

接下来我使用了ggpairs(samples, lower=list(continuous="density"))GGally(感谢@LucianoSelzer 在下面发表评论)。现在我们有上角的相关性和下角的密度,但是我们缺少对角线条形图,并且密度图不是热图形状的。

有什么想法可以使图片更接近所需的图片(第一张)?

【问题讨论】:

    标签: r heatmap density-plot ggally


    【解决方案1】:

    您可以尝试组合几种不同的绘图方法并组合结果。这是一个示例,可以进行相应调整:

    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:更正了最后一个循环的索引。

    【讨论】:

    • 哇!太好了,谢谢。我很想知道是否还有任何伟大而简短的 ggplot2 答案。
    • 我敢打赌,我刚刚开始熟悉 ggplot2,所以我决定走老路。 ggplot2 使用网格图形,所以布局想法在那里不起作用。但这可能会有所帮助:cookbook-r.com/Graphs/Multiple_graphs_on_one_page_%28ggplot2%29
    【解决方案2】:

    你可以使用三个不同的包和两个不同的函数来做这样的事情,如下所示:

    cor_fun 用于上三角相关计算。 my_fn 用于下三角绘图

    你还需要ggpairs

    library(GGally)
    library(ggplot2)
    library(RColorBrewer)
    
    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))
    
    cor_fun <- function(data, mapping, method="pearson", ndp=2, sz=5, stars=TRUE){ #ndp is to adjust the number of decimals
      
      x <- eval_data_col(data, mapping$x)
      y <- eval_data_col(data, mapping$y)
      
      corr <- cor.test(x, y, method=method)
      est <- corr$estimate
      lb.size <- sz 
      
      if(stars){
        stars <- c("***", "**", "*", "")[findInterval(corr$p.value, c(0, 0.001, 0.01, 0.05, 1))]
        lbl <- paste0(round(est, ndp), stars)
      }else{
        lbl <- round(est, ndp)
      }
      
      ggplot(data=data, mapping=mapping) +
        annotate("text", x=mean(x, na.rm=TRUE), y=mean(y, na.rm=TRUE), label=lbl, size=lb.size)+
        theme(panel.grid = element_blank(), panel.background=element_rect(fill="snow1")) 
      
    }
    
    colfunc<-colorRampPalette(c("darkblue","cyan","yellow","red"))
    
    my_fn <- function(data, mapping){
      p <- ggplot(data = data, mapping = mapping) + 
        stat_density2d(aes(fill=..density..), geom="tile", contour = FALSE) +
        scale_fill_gradientn(colours = colfunc(100)) + theme_classic()
    }
    
    
    ggpairs(samples, columns = c(1,2,3,4,5),
            lower=list(continuous=my_fn),
            diag=list(continuous=wrap("densityDiag", fill="gray92")), #densityDiag is a function
            upper=list(continuous=cor_fun)) + theme(panel.background=element_rect(fill="white")) +
      theme(axis.text.x = element_text(angle = 0, vjust = 1, color = "black")) + 
      theme(axis.text.y = element_text(angle = 0, vjust = 1 , color = "black"))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-07
      • 2012-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-11
      • 2021-11-13
      相关资源
      最近更新 更多