ggcorrplot绘制相关性热图

发布于 2022-08-09  108 次阅读


安装包

  • conda activate wgcna
  • conda install -c conda-forge r-ggcorrplot -y
  • conda install -c conda-forge r-ggsci -y
  • # conda install -c conda-forge r-gridextra -y

颜色参考

library("reshape2")

set.seed(42)
k <- 9
x <- diag(k)
x[upper.tri(x)] <- runif(sum(1:(k - 1)), 0, 1)
x_melt <- melt(x)

p4 <- ggplot(x_melt, aes(x = Var1, y = Var2, fill = value)) +
  geom_tile(colour = "black", size = 0.3) +
  scale_x_continuous(expand = c(0, 0)) +
  scale_y_continuous(expand = c(0, 0)) +
  theme_bw() + theme(
    legend.position = "none", plot.background = element_blank(),
    axis.line = element_blank(), axis.ticks = element_blank(),
    axis.text.x = element_blank(), axis.text.y = element_blank(),
    axis.title.x = element_blank(), axis.title.y = element_blank(),
    panel.background = element_blank(), panel.border = element_blank(),
    panel.grid.major = element_blank(), panel.grid.minor = element_blank()
  )
 
options(repr.plot.width=12, repr.plot.height=8)
grid.arrange(
  p4 + scale_fill_material("red"), p4 + scale_fill_material("pink"),
  p4 + scale_fill_material("purple"), p4 + scale_fill_material("deep-purple"),
  p4 + scale_fill_material("indigo"), p4 + scale_fill_material("blue"),
  p4 + scale_fill_material("light-blue"), p4 + scale_fill_material("cyan"),
  p4 + scale_fill_material("teal"), p4 + scale_fill_material("green"),
  p4 + scale_fill_material("light-green"), p4 + scale_fill_material("lime"),
  p4 + scale_fill_material("yellow"), p4 + scale_fill_material("amber"),
  p4 + scale_fill_material("orange"), p4 + scale_fill_material("deep-orange"),
  p4 + scale_fill_material("brown"), p4 + scale_fill_material("grey"),
  p4 + scale_fill_material("blue-grey"),p4 + scale_fill_gsea(),
  ncol = 6
)

相关性热图

library(Hmisc)
library(ggcorrplot)
library(ggsci)
library(gridExtra)
f_corrplot <- function(lc_scdata, scale=T, lab=F){
    lc_cor <- rcorr(lc_scdata)
    lc_cor$P[is.na(lc_cor$P)]=0
    if(scale){lc_cor$r[lc_cor$r<0] <- lc_cor$r[lc_cor$r<0] - (1 + min(lc_cor$r))}
    ggcorrplot(lc_cor$r, type="full",hc.order = T, lab = lab, p.mat = lc_cor$P)
}
options(repr.plot.width=8, repr.plot.height=8)
p <- f_corrplot(as.matrix(datExpr)) + scale_fill_material("cyan")
p

医学生