今天小果想学习利用linkET包来绘制mantel test相关性热图,代码如下:
- 安装需要的R包
install.packages(“ggplot2”)
install.packages(“tidyverse”)
install.packages(“devtools”)
devtools::install_github(“Hy4m/linkET”, force = TRUE)
- 载入需要的R包
library(devtools)
library(ggplot2)
library(tidyverse)
library(linkET)
- 代码展示
#读取数据
varechem<-read.csv(file=”varechem.csv”,row.names=1)
varespec<-read.csv(file=”varespec.csv”,row.names=1)
# mantel test
mantel <- mantel_test(varespec, varechem, # 传入需要检测的两个矩阵
mantel_fun = ‘mantel’,
spec_select = list(Spec01 = 1:7,
Spec02 = 8:18,
Spec03 = 19:37,
Spec04 = 38:44)) %>%
mutate(r = cut(r, breaks = c(-Inf, 0.2, 0.4, Inf),
labels = c(“< 0.2”, “0.2 – 0.4”, “>= 0.4”)),
p = cut(p, breaks = c(-Inf, 0.01, 0.05, Inf),
labels = c(“< 0.01”, “0.01 – 0.05”, “>= 0.05”)))
#计算环境因子相关性系数:
cor2 <- correlate(varechem)
#绘制相关性热图
p1<- qcorrplot(cor2,
grid_col = “grey50”,
grid_size = 0.2,
type = “upper”,
diag = FALSE) +
geom_square() +
scale_fill_gradientn(colours = c(“#610214”, “#d05646”, “#f5f4f4”, “#569cc7”, “#0b3b71”
),limits = c(-1, 1))
#添加显著性标签:
p2 <- p1+
geom_mark(size = 4,
only_mark = T,
sig_level = c(0.05, 0.01, 0.001),
sig_thres = 0.05,
colour = ‘white’)
#在相关性热图上添加mantel连线:
p3<- p2+
geom_couple(data = mantel,
aes(colour = p, size = r),
curvature = nice_curvature())+
scale_size_manual(values = c(0.5, 1.2, 2)) + #连线粗细
scale_colour_manual(values = c(“#d85c01”, “#29d300”, “#A2A2A288”
)) + #连线配色
#修改图例:
guides(size = guide_legend(title = “Mantel r”,
override.aes = list(colour = “grey35”),
order = 2),
colour = guide_legend(title = “Mantel p”,
override.aes = list(size = 3),
order = 1),
fill = guide_colorbar(title = “Pearson r”, order = 3))
小果感觉效果还不错,该包与ggplot2语法兼容,在绘图的同时可以学习相关的ggplot2的绘图知识,值得推荐,今天小果的分享就到这里了,有需要的可以借鉴学习,下期再见。