哪些微生物和代谢物是正相关/负相关的关系,先用Spearman相关系数看一看。
library(psych)
library(pheatmap)
library(RColorBrewer)
my_theme()
# 随机模拟数据
metab <- data.frame(matrix(c(rnorm(20, 0, 1),
rnorm(20, 1, 2),
rnorm(20, 2, 3),
rnorm(20, 4, 3),
rnorm(20, 3, 4)),
20,5))
colnames(metab) <- paste0("S", seq(1,5))
rownames(metab) <- c(paste0("OTU", seq(1,15)), paste0("metab",seq(1,5)))
cor_mat <- corr.test(t(metab), method = "spearman")
cor_mat_r <- data.frame(cor_mat$r[16:20, 1:15])
cor_mat_p <- data.frame(cor_mat$p[16:20, 1:15])
p_mat <- matrix('', nrow(cor_mat_p), ncol(cor_mat_p))
p_mat[cor_mat_p < 0.05 & cor_mat_p >= 0.01] <- "*"
p_mat[cor_mat_p < 0.01] <- "**"
colnames(p_mat) <- colnames(cor_mat_p)
rownames(p_mat) <- rownames(cor_mat_p)
# 列注释
anno_col <- data.frame(Phylum = factor(rep(c("Proteobacteria", "Bacteriodetes", "Firmicutes"),
c(6,6,3))))
rownames(anno_col) <- colnames(cor_mat_r)
# 行注释
anno_row <- data.frame(MetaClass = factor(rep(c("A","B","C"), c(1,2,2))))
rownames(anno_row) <- rownames(cor_mat_r)
# 颜色
anno_color <- list(Phylum = c(Bacteriodetes = "#e63946",
Firmicutes = "#f1faee",
Proteobacteria = "#a8dadc"),
MetaClass = c(A = "#006d77",
B = "#83c5be",
C = "#edf6f9"))
# 出图
pheatmap(cor_mat_r,
display_numbers = p_mat,
number_color = "black",
cluster_cols = T,
cluster_rows = T,
color = colorRampPalette(rev(brewer.pal(n = 7,
name ="PRGn")))(100),
annotation_col = anno_col,
annotation_row = anno_row,
annotation_colors = anno_color
)
注: my_theme
function() {
library(ggplot2)
library(ggthemr)
theme_set(theme_classic() +
theme(axis.text = element_text(size = 8, face = "bold"), #坐标轴标签大小,加粗
axis.title = element_text(size = 12, face = "bold"), #坐标轴标题大小,加粗
plot.margin = unit(rep(1, 4), "cm"), #边距,顺序:上右下左
panel.grid = element_blank())) #不要横向和纵向格子线
}
部分参考自: https://www.sohu.com/a/457132483_120055884