目的
可以较为直观比较多个因子与组内的所有因子之间的相关性。

代码
setwd("D:/生信代码复现/矩阵相关性分析") #💚💚💚💚工作目录(需修改)
# 加载所需库
library(Hmisc)
library(dplyr)
library(linkET)
library(ggplot2)
# 读取数据
df <- read.csv(file.choose(), header = TRUE, row.names = 1)
# 提取目标因子和环境因子
target_factors <- c("AGB", "BGB") #💚💚💚💚这个改成你想要比较的参数,可以多个
env_factors <- setdiff(colnames(df), target_factors)
# 分离环境因子数据
env <- df[, env_factors]
# 计算相关性矩阵和 P 值矩阵
df_rcorr <- rcorr(as.matrix(df), type = "pearson")
r <- df_rcorr$r # 相关系数矩阵
p <- df_rcorr$P # P 值矩阵
# 初始化结果数据框
results <- data.frame(P = character(), R = numeric(), Y = character(), X = character(), sig = character(), type = character(), stringsAsFactors = FALSE)
# 遍历每个目标因子和环境因子的组合,计算相关性
for (y in target_factors) {
for (x in env_factors) {
r_value <- r[x, y]
p_value <- p[x, y]
# 确定显著性水平和相关性方向
significance <- ifelse(p_value < 0.05, "< 0.05", ">= 0.05")
sig <- ifelse(r_value > 0, ">0", "<0")
type <- ifelse(r_value > 0, "实线", "虚线")
# 添加结果到数据框
results <- rbind(results, data.frame(P = significance, R = round(r_value, 2), Y = y, X = x, sig = sig, type = type))
}
}
# 查看结果
print(results)
# 设置颜色映射
cols <- c(">= 0.05" = "grey", "< 0.05" = "#1B9E77", "< 0.01" = "#D95F02")
# 绘制相关性图,带显著性标记
qcorrplot(correlate(env), type = "lower", diag = FALSE) +
geom_square() +
geom_text(aes(label = ifelse(p < 0.001, "***",
ifelse(p < 0.01, "**",
ifelse(p < 0.05, "*", "")))),
size = 5, color = "black") + # 根据 p 值设置星号
geom_couple(aes(
colour = P, # 使用显著性 P 值来设置线的颜色
size = abs(R), # 根据相关系数 R 的绝对值调整线条粗细
linetype = type, # 根据 type 列选择实线或虚线
from = Y, to = X # from 和 to 指定因变量和环境因子
), data = results, curvature = 0.15) +
scale_fill_gradientn(colours = RColorBrewer::brewer.pal(3, "RdBu")) +
scale_size_continuous(range = c(0.5, 2)) + # 设置线条粗细范围
scale_colour_manual(values = cols) +
scale_linetype_manual(values = c("实线" = "solid", "虚线" = "dashed")) + # 设置线型映射
guides(
size = guide_legend(title = "Correlation Strength",
override.aes = list(colour = "grey35"), order = 2),
colour = guide_legend(title = "P value",
override.aes = list(size = 3), order = 1),
fill = guide_colorbar(title = "Pearson's r", order = 3),
linetype = guide_legend(title = "Line Type", order = 4)
) +
theme_minimal() +
theme(panel.grid = element_blank(), # 移除网格
axis.text.x = element_text(angle = 45, hjust = 1)) + # x轴标签旋转
labs(title = "Environmental Factors Correlation with Biomass",
x = "Environmental Factor (X)", y = "Target Factor (Y)")
复现出来的图片

代码
remove(list = ls()) ##清空当前环境
setwd("D:/生信代码复现/相关性分析/复现文件") ##设置路径
library(dplyr)
library(linkET)
library(ggplot2)
speciese <- read.csv(file.choose(),header = T,row.names = NULL)
env <- read.csv(file.choose(),header = T,row.names = NULL)
mantel01 <- mantel_test(speciese, env,
spec_select = list(Y1 = 1:7,
Y2 = 8:18,
Y3 = 19:37,
Y4 = 38:44
)) %>%
mutate(rd = cut(r, breaks = c(-Inf, 0.2, 0.4, Inf),
labels = c("< 0.2", "0.2 - 0.4", ">= 0.4")),
pd = cut(p, breaks = c(-Inf, 0.01, 0.05, Inf),
labels = c("< 0.01", "0.01 - 0.05", ">= 0.05")))
qcorrplot(correlate(env),
type = "lower",
diag = FALSE,
) +
geom_square() +
geom_couple(aes(colour = pd, size = rd),data = mantel01, curvature = 0.1,
node.colour = c("blue", "blue"),
node.fill = c("grey", "grey"),
node.size = c(3.5, 2.5),
) +
scale_fill_gradientn(colours = RColorBrewer::brewer.pal(11, "RdBu"),
limits = c(-1, 1),
breaks = seq(-1,1,0.5)) +
geom_mark(size=2.5,
only_mark=T,
sig_level=c(0.05,0.01,0.001),
sig_thres=0.05)+
scale_size_manual(values = c(0.5, 1, 1.5, 2)) +
scale_colour_manual(values = color_pal(3)) +
guides(size = guide_legend(title = "Mantel's r",
override.aes = list(colour = "grey35"),
order = 2),
colour = guide_legend(title = "Mantel's P",
override.aes = list(size = 1.5),
order = 1),
fill = guide_colorbar(title = "Pearson's r", order = 3))
ggsave("Mantel test.tiff",width = 8,height = 6)
复现的图片
