干货分享 | 相关性组合图绘制

文摘   2024-11-22 20:04   北京  

写在前面

本公众号之前推送过关于怎么使用mantel检验的绘图形式绘制普通相关性(pearson、spearman之类的)推文但存在不完善的地方,需要大家手动整理相关性的结果。本篇推文为升级版代码,能够实现一键出图。

准备数据集

数据如下,小编已上传,文末下载。

直接开始绘图

我们只需要提及几个小细节,这里我们记住第11行代码,这里是选择自己绘图的目标因子的,想选择几个都可以。
# 加载所需库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·

图文编辑:王淑巾 武园伊

审编:周笑语 闫孟蕊

终审:初明若 李雨竹 代浩宇 毕丝淇


猜你喜欢

1.佳文赏析 | 李立华等:创造力空间:重新认识文化和旅游的本质

2.佳文集锦 | 博物馆奇妙游:探索游客体验之旅

3.专题征稿 | 城市洪涝灾害

4.地学招聘 | 新奥尔良大学规划与城市研究系田光副教授招收全奖博士生

地研联
中国地理学会研究生联合分会是全国各高校和科研院所的地理学研究生会自愿联合、促进交流、互相协助、共同进步的公益性二级学术组织,系中国地理学会分支机构之一。
 最新文章