大家好,我是带我去滑雪!
热图和网络图分别展示了数据的不同方面。热图可用于显示变量之间的相关性或模式,而网络图则可用于显示节点之间的连接关系。通过将它们组合在一起,可以更全面地展示数据之间的关系和结构。下面开始代码实战。
(1)安装并调用相关R包
install.packages("vegan",repos="http://mirrors.tuna.tsinghua.edu.cn/CRAN/")
install.packages("ggcor",repos="http://mirrors.tuna.tsinghua.edu.cn/CRAN/")
#安装devtools
install.packages("devtools")
#安装ggcor
devtools::install_local("F:/R-4.2.2/library/ggcor_master/ggcor-1-master", force = TRUE)
library(vegan) # Community Ecology Package
library(dplyr) # A Grammar of Data Manipulation
library(ggcor) # Extended tools for correlation analysis and visualization
library(ggplot2) # Create Elegant Data Visualisations Using the Grammar of Graphics
(2)导入数据
varechem<-read.csv("E:/工作/硕士/博客/博客粉丝问题/新建文件夹/mantel test/varechem.csv",header=TRUE,sep=",",fileEncoding = "GBK")
varespec<-read.csv("E:/工作/硕士/博客/博客粉丝问题/新建文件夹/mantel test/varespec.csv",header=TRUE,sep=",",fileEncoding = "GBK")
varechem
varespec
varechem部分数据展示:
X N P K Ca Mg S Al Fe Mn Zn Mo Baresoil Humdepth pH
1 18 19.8 42.1 139.9 519.4 90.0 32.3 39.0 40.9 58.1 4.5 0.30 43.90 2.2 2.7
2 15 13.4 39.1 167.3 356.7 70.7 35.2 88.1 39.0 52.4 5.4 0.30 23.60 2.2 2.8
3 24 20.2 67.7 207.1 973.3 209.1 58.1 138.0 35.4 32.1 16.8 0.80 21.20 2.0 3.0
4 27 20.6 60.8 233.7 834.0 127.2 40.7 15.4 4.4 132.0 10.7 0.20 18.70 2.9 2.8
5 23 23.8 54.5 180.6 777.0 125.8 39.5 24.2 3.0 50.1 6.6 0.30 46.00 3.0 2.7
6 19 22.8 40.9 171.4 691.8 151.4 40.8 104.8 17.6 43.6 9.1 0.40 40.50 3.8 2.7
7 22 26.6 36.7 171.4 738.6 94.9 33.8 20.7 2.5 77.6 7.4 0.30 23.00 2.8 2.8
8 16 24.2 31.0 138.2 394.6 45.3 27.1 74.2 9.8 24.4 5.2 0.30 29.80 2.0 2.8
9 28 29.8 73.5 260.0 748.6 105.3 42.5 17.9 2.4 106.6 9.3 0.30 17.60 3.0 2.8
10 13 28.1 40.5 313.8 540.7 118.9 60.2 329.7 109.9 61.7 9.1 0.50 29.90 2.2 2.8
11 14 21.8 38.1 146.8 512.2 75.0 36.6 92.3 4.6 29.0 8.1 0.50 33.30 2.7 2.7
12 20 26.2 61.9 202.2 741.2 86.3 48.6 124.3 23.6 94.5 10.2 0.60 56.90 2.5 2.9
13 25 22.8 50.6 151.7 648.0 64.8 30.2 12.1 2.3 122.9 8.1 0.20 23.70 2.6 2.9
14 7 30.5 24.6 78.7 188.5 55.5 25.3 294.9 123.8 10.1 3.0 0.40 18.60 1.7 3.1
15 5 33.1 22.7 43.6 240.3 25.7 14.9 39.0 8.4 26.8 8.4 0.20 8.10 1.0 3.1
16 6 19.1 26.4 61.1 259.1 37.0 21.4 155.1 81.4 20.6 4.0 0.60 5.80 1.9 3.0
varespec部分数据展示:
(3)绘制组合图
mantel <- mantel_test(varespec, varechem,
spec.select = list(Spec01 = 1:7,
Spec02 = 8:18,
Spec03 = 19:37,
Spec04 = 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.value, breaks = c(-Inf, 0.01, 0.05, Inf),
labels = c("< 0.01", "0.01 - 0.05", ">= 0.05")))
quickcor(varechem, type = "upper") +
geom_square() +
anno_link(aes(colour = pd, size = rd), data = mantel) +
scale_size_manual(values = c(0.5, 1, 2)) +
scale_colour_manual(values = c("#D95F02", "#1B9E77", "#A2A2A288")) +
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 = 3),
order = 1),
fill = guide_colorbar(title = "Pearson's r", order = 3))
图形效果:
需要数据集的家人们可以去百度网盘(永久有效)获取:
链接:https://pan.baidu.com/s/173deLlgLYUz789M3KHYw-Q?pwd=0ly6
提取码:2138
更多优质内容持续发布中,请移步主页查看。
若有问题可邮箱联系:1736732074@qq.com
博主的WeChat:TCB1736732074
点赞+关注,下次不迷路!