R 几种分布的成对图形比较

R 几种分布的成对图形比较,r,heatmap,density-plot,ggally,R,Heatmap,Density Plot,Ggally,这是前一个问题的编辑版本 我们得到了一个由n个观测值(样本)和m个变量(基因等)组成的m×n表,我们希望研究每对观测值之间变量的行为——例如,具有最高正相关或负相关的两个观测值。为此,我在Stadler等人的著作中看到了一张很棒的图表。《自然》杂志(2011年): 在这里,它可以是要使用的示例数据集 m <- 1000 samples <- data.frame(unif1 = runif(m), unif2 = runif(m, 1, 2), norm1 = rnorm(m),

这是前一个问题的编辑版本

我们得到了一个由n个观测值(样本)和m个变量(基因等)组成的m×n表,我们希望研究每对观测值之间变量的行为——例如,具有最高正相关或负相关的两个观测值。为此,我在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))

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

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))
    } 

cors您可以使用以下三种不同的软件包和两种不同的功能执行类似操作:

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"))
库(GGally)
图书馆(GG2)
图书馆(RColorBrewer)

哇!太好了,谢谢你。我很好奇,看看是否还有什么好的、简短的ggplot2答案。我敢肯定有,我刚刚开始熟悉ggplot2,所以我决定走老路。ggplot2使用网格图形,因此布局思想在那里不起作用。但这可能会有所帮助: