写在前面
准备数据集
直接开始绘图
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.1, 1)) + # 设置线条粗细范围
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)")
完整版代码如下
# 加载所需库
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.1, 1)) + # 设置线条粗细范围
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)")
★
转载自生态R学社
文章仅代表作者观点,与本公众号无关,版权归原作者所有
原文标题:相关性组合图最终版
·END·
图文编辑:王淑巾 武园伊
审编:周笑语 闫孟蕊
终审:初明若 李雨竹 代浩宇 毕丝淇
猜你喜欢