【R语言】随机森林+相关性热图组合图

数据概况文末有获取方式

随机森林部分

#调用R包
library(randomForest)
library(rfPermute)
library(ggplot2)
library(psych)
library(reshape2)
library(patchwork)
library(reshape2)
library(RColorBrewer)
​
​
#读取数据
df<-read.csv("F:\\EXCEL-元数据\\2020中.csv")
​
#设置随机种子,使结果能够重现
set.seed(123)
​
#运行随机森林
rf_results<-rfPermute(ESI~., data =df, importance = TRUE, ntree = 500)
​#查看随机森林主要结果
rf_results$rf
​
#提取预测因子的解释率
predictor_var<- data.frame(importance(rf_results, scale = TRUE), check.names = FALSE)
​
#提取预测变量的显著
predictor_sig<-as.data.frame((rf_results$pval)[,,2])
colnames(predictor_sig)<-c("sig","other")
​
#合并显著因子和解释率表
df_pre<-cbind(predictor_var,predictor_sig)
df_pre$sig[df_pre$sig<=0.05]<-"*"
df_pre$sig[df_pre$sig>=0.05]<-" "
k <- df_pre$IncNodePurity[df_pre$sig=="*"]<-"#99c1e1"
​
# 自定义顺序列表
custom_order <- c("TEM", "PRE", "NDVI", "DEM", "SLOPE","LANDUSE","NPP","POP","GDP")
# 创建行名列
df_pre$rowname <- factor(rownames(df_pre), levels = custom_order)

绘制随机森林条形图

# 绘制柱状图,使用自定义顺序
p1 <- ggplot(data=df_pre, aes(x=`%IncMSE`, y=rowname)) +geom_bar(stat='identity', width=0.6,fill=k) +#geom_errorbar(aes(xmin=mean-sd, xmax=mean+sd), width = 0.2, color = "black") +theme_classic() + labs(x='Increase in MSE(%)', y='') +scale_x_continuous(limits = c(min(0), max(df_pre$`%IncMSE`) + 10),breaks=seq(0,max(df_pre$`%IncMSE`) + 10,20),expand = c(0, 0)) +geom_text(aes(label=sig, x=`%IncMSE` + 0.1, y=rowname), hjust=-0.3, size=6) +theme(axis.text.y = element_blank())+theme(axis.text=element_text(color="black", size=11),axis.ticks.y = element_blank(),panel.grid.major.y = element_blank(),panel.grid.minor.y = element_blank(),panel.border = element_blank(),panel.background = element_blank())
p1

结果如下:

相关性热图部分

#读取环境变量和影响因子矩阵
env <- df$ESI
spe <- df[,-1]
#spe <- spe[rownames(env), ]
####环境变量和影响因子的相关性分析
library(psych)
library(reshape2)
#可通过 psych 包函数 corr.test() 执行
#这里以 pearson 相关系数为例,暂且没对 p 值进行任何校正(可以通过 adjust 参数额外指定 p 值校正方法)
pearson <- corr.test(env, spe, method = 'pearson', adjust = 'none')
r <- data.frame(pearson$r)  #pearson 相关系数矩阵
p <- data.frame(pearson$p)  #p 值矩阵
#结果整理以便于作图
r$env <- rownames(r)
p$env <- rownames(p)
r <- melt(r, id = 'env')
p <- melt(p, id = 'env')
pearson <- cbind(r, p$value)
colnames(pearson) <- c('env', 'spe', 'pearson_correlation', 'p.value')
pearson$spe <- factor(pearson$spe, levels = colnames(spe))
head(pearson)  
​
#ggplot2 作图,绘制环境变量和因子的 pearson 相关性热图
​
# 自定义顺序列表
custom_order <- c("TEM", "PRE", "NDVI", "DEM", "SLOPE","LANDUSE","NPP","POP","GDP")
​
# 将 'env' 列转换为因子,并按照自定义顺序排序
pearson$env <- factor(pearson$env, levels = custom_order)

出图

# 使用排序后的数据绘制热图
p2 <- ggplot() +geom_tile(data = pearson, aes(x = env, y = spe, fill = pearson_correlation)) +scale_fill_gradientn(colors = c('#3e689d', '#e8f0db', '#b0271f'), limit = c(-1, 1)) +theme(panel.grid = element_line(), panel.background = element_rect(color = 'black'), legend.key = element_blank(), legend.position = "bottom",#legend.margin = margin(t = -1, unit = "cm"),  # 调整图例和图的上方间距#legend.box.margin = margin(t = 0, unit = "cm"),  # 调整图例内部内容的上方间距axis.text.x = element_text(color = 'black', angle = 45, hjust = 1, vjust = 1), axis.text.y = element_text(color = 'black',size=12), axis.ticks = element_line(color = 'black')) +scale_x_discrete(expand = c(0, 0)) +scale_y_discrete(expand = c(0, 0)) +coord_fixed(ratio=1) +theme(axis.text.x = element_blank(),axis.ticks.x = element_blank(),panel.grid.major.x = element_blank(),panel.grid.minor.x = element_blank(),panel.border = element_blank(),panel.background = element_blank())+labs(y = '', x = '', fill = '')
​
p2
​
#如果想把 pearson 相关系数的显著性也标记在图中,参考如下操作
pearson[which(pearson$p.value<0.001),'sig'] <- '***'
pearson[which(pearson$p.value<0.01 & pearson$p.value>0.001),'sig'] <- '**'
pearson[which(pearson$p.value<0.05 & pearson$p.value>0.01),'sig'] <- '*'
head(pearson)  #整理好的环境变量和物种丰度的 pearson 相关性统计表
​
p3 <- p2 +geom_text(data = pearson, aes(x = env, y = spe, label = sig), size = 6)
​
p3

合并两幅图,并导出

p3<-p3+theme(plot.margin = margin(0,0,0,0))  # 分别为上、右、下、左
p1<-p1+theme(plot.margin = margin(0,0,0,0))
​
p3+p1
​
#保存至ppt
library(eoffice)
topptx(filename = "F:\\出图\\2020中.pptx",height=5,width=3)

完整代码

#下载r包
install.packages("rfPermute")
install.packages("ggplot2")
install.packages("psych")
install.packages("reshape2")
install.packages("patchwork")
install.packages("randomForest")
​
#调用R包
library(randomForest)
library(rfPermute)
library(ggplot2)
library(psych)
library(reshape2)
library(patchwork)
library(reshape2)
library(RColorBrewer)
​
​
#读取数据
df<-read.csv("F:\\EXCEL-元数据\\2020中.csv")
​
#设置随机种子,使结果能够重现
set.seed(123)
​
#运行随机森林
rf_results<-rfPermute(ESI~., data =df, importance = TRUE, ntree = 500)
​#查看随机森林主要结果
rf_results$rf
​
#提取预测因子的解释率
predictor_var<- data.frame(importance(rf_results, scale = TRUE), check.names = FALSE)
​
#提取预测变量的显著
predictor_sig<-as.data.frame((rf_results$pval)[,,2])
colnames(predictor_sig)<-c("sig","other")
​
#合并显著因子和解释率表
df_pre<-cbind(predictor_var,predictor_sig)
df_pre$sig[df_pre$sig<=0.05]<-"*"
df_pre$sig[df_pre$sig>=0.05]<-" "
k <- df_pre$IncNodePurity[df_pre$sig=="*"]<-"#99c1e1"
​
# 自定义顺序列表
custom_order <- c("TEM", "PRE", "NDVI", "DEM", "SLOPE","LANDUSE","NPP","POP","GDP")
# 创建行名列
df_pre$rowname <- factor(rownames(df_pre), levels = custom_order)
​
​
​
# 绘制柱状图,使用自定义顺序
p1 <- ggplot(data=df_pre, aes(x=`%IncMSE`, y=rowname)) +geom_bar(stat='identity', width=0.6,fill=k) +#geom_errorbar(aes(xmin=mean-sd, xmax=mean+sd), width = 0.2, color = "black") +theme_classic() + labs(x='Increase in MSE(%)', y='') +scale_x_continuous(limits = c(min(0), max(df_pre$`%IncMSE`) + 10),breaks=seq(0,max(df_pre$`%IncMSE`) + 10,20),expand = c(0, 0)) +geom_text(aes(label=sig, x=`%IncMSE` + 0.1, y=rowname), hjust=-0.3, size=6) +theme(axis.text.y = element_blank())+theme(axis.text=element_text(color="black", size=11),axis.ticks.y = element_blank(),panel.grid.major.y = element_blank(),panel.grid.minor.y = element_blank(),panel.border = element_blank(),panel.background = element_blank())
p1
​
​
#读取环境变量和物种丰度矩阵
env <- df$ESI
spe <- df[,-1]
#spe <- spe[rownames(env), ]
####环境变量和物种丰度的相关性分析
library(psych)
library(reshape2)
#可通过 psych 包函数 corr.test() 执行
#这里以 pearson 相关系数为例,暂且没对 p 值进行任何校正(可以通过 adjust 参数额外指定 p 值校正方法)
pearson <- corr.test(env, spe, method = 'pearson', adjust = 'none')
r <- data.frame(pearson$r)  #pearson 相关系数矩阵
p <- data.frame(pearson$p)  #p 值矩阵
#结果整理以便于作图
r$env <- rownames(r)
p$env <- rownames(p)
r <- melt(r, id = 'env')
p <- melt(p, id = 'env')
pearson <- cbind(r, p$value)
colnames(pearson) <- c('env', 'spe', 'pearson_correlation', 'p.value')
pearson$spe <- factor(pearson$spe, levels = colnames(spe))
head(pearson)  #整理好的环境变量和物种丰度的 pearson 相关性统计表
​
#ggplot2 作图,绘制环境变量和物种丰度的 pearson 相关性热图
​
# 自定义顺序列表
custom_order <- c("TEM", "PRE", "NDVI", "DEM", "SLOPE","LANDUSE","NPP","POP","GDP")
​
# 将 'env' 列转换为因子,并按照自定义顺序排序
pearson$env <- factor(pearson$env, levels = custom_order)
​
# 使用排序后的数据绘制热图
p2 <- ggplot() +geom_tile(data = pearson, aes(x = env, y = spe, fill = pearson_correlation)) +scale_fill_gradientn(colors = c('#3e689d', '#e8f0db', '#b0271f'), limit = c(-1, 1)) +theme(panel.grid = element_line(), panel.background = element_rect(color = 'black'), legend.key = element_blank(), legend.position = "bottom",#legend.margin = margin(t = -1, unit = "cm"),  # 调整图例和图的上方间距#legend.box.margin = margin(t = 0, unit = "cm"),  # 调整图例内部内容的上方间距axis.text.x = element_text(color = 'black', angle = 45, hjust = 1, vjust = 1), axis.text.y = element_text(color = 'black',size=12), axis.ticks = element_line(color = 'black')) +scale_x_discrete(expand = c(0, 0)) +scale_y_discrete(expand = c(0, 0)) +coord_fixed(ratio=1) +theme(axis.text.x = element_blank(),axis.ticks.x = element_blank(),panel.grid.major.x = element_blank(),panel.grid.minor.x = element_blank(),panel.border = element_blank(),panel.background = element_blank())+labs(y = '', x = '', fill = '')
​
p2
​
#如果想把 pearson 相关系数的显著性也标记在图中,参考如下操作
pearson[which(pearson$p.value<0.001),'sig'] <- '***'
pearson[which(pearson$p.value<0.01 & pearson$p.value>0.001),'sig'] <- '**'
pearson[which(pearson$p.value<0.05 & pearson$p.value>0.01),'sig'] <- '*'
head(pearson)  #整理好的环境变量和物种丰度的 pearson 相关性统计表
​
p3 <- p2 +geom_text(data = pearson, aes(x = env, y = spe, label = sig), size = 6)
​
p3
​
​
p3<-p3+theme(plot.margin = margin(0,0,0,0))  # 分别为上、右、下、左
p1<-p1+theme(plot.margin = margin(0,0,0,0))
​
p3+p1
​
#保存至ppt
library(eoffice)
topptx(filename = "F:\\出图\\2020中.pptx",height=5,width=3)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/882036.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

深度学习之残差网络ResNet

文章目录 1. 残差网络定义2. 数学基础函数类3. 残差块4.ResNet模型5.训练模型6.小结 1. 残差网络定义 随着我们设计的网络越来越深&#xff0c;深刻理解“新添加的层如何提升神经网络的性能”变得至关重要。更重要的是设计网络的能力。在这种网络中&#xff0c;添加层会使得网…

2010年国赛高教杯数学建模A题储油罐的变位识别与罐容表标定解题全过程文档及程序

2010年国赛高教杯数学建模 A题 储油罐的变位识别与罐容表标定 通常加油站都有若干个储存燃油的地下储油罐&#xff0c;并且一般都有与之配套的“油位计量管理系统”&#xff0c;采用流量计和油位计来测量进/出油量与罐内油位高度等数据&#xff0c;通过预先标定的罐容表&#…

JDBC的学习

一、JDBC DriverManager 二、JDBC connection 三、 JDBC Statement 1.DML 2.DDL 四、JDBC ResultSet 五、JDBC PreparedStatement

30.第二阶段x86游戏实战2-遍历周围-C++遍历二叉树(玩家角色基址)

免责声明&#xff1a;内容仅供学习参考&#xff0c;请合法利用知识&#xff0c;禁止进行违法犯罪活动&#xff01; 本次游戏没法给 内容参考于&#xff1a;微尘网络安全 本人写的内容纯属胡编乱造&#xff0c;全都是合成造假&#xff0c;仅仅只是为了娱乐&#xff0c;请不要…

Prometheus运维监控平台之监控指标注册到consul脚本开发、自定义监控项采集配置调试(三)

系列文章目录 运维监控平台搭建 运维监控平台监控标签 golang_Consul代码实现Prometheus监控目标的注册以及动态发现与配置V1版本 文章目录 系列文章目录目的一、监控指标注册到consul的golang脚本开发1、修改settings.yaml文件2、修改config/ocnsul,go文件3、修改core/consul…

让你的MacOS剪切板变得更加强大,如何解决复制内容覆盖的问题

MacOS的日常使用过程中&#xff0c;肯定少不了复制粘贴&#xff0c;不论是文本内容还是文件&#xff0c;复制粘贴是避不开的操作&#xff0c;如果需要复制粘贴的内容不多&#xff0c;那么普通的复制粘贴就可以完成了&#xff0c;但是当有同样的内容需要输入不同的地方的时候&am…

C++的魔法世界:类和对象的终章

文章目录 一、再探构造函数二、类型转换2.1隐式类型转换2.2内置类型的类型转化2.3explicit关键字2.4多参数构造 三、static成员四、友元五、内部类内部类的特性 六、匿名对象 一、再探构造函数 类和对象(中)里介绍的构造函数&#xff0c;使用的是赋值实现成员变量的初始化。而…

出现接地故障电流现象,安科瑞ASJ剩余电流继电器可以避免吗?

什么是ASJ剩余电流继电器 剩余电流继电器是检测剩余电流&#xff0c;并将剩余电流值与基准值相比较的电器。当剩余电流值超过基准值时&#xff0c;它会发出一个机械开闭信号&#xff0c;使机械开关电器脱扣或声光报警装置发出报警。这种继电器通常基于漏电保护原理工作&#x…

【QAMISRA】解决导入commands.json时报错问题

【更多软件使用问题请点击亿道电子官方网站】 1、 文档目标 解决导入commands.json时报错“Could not obtain system-wide includes and defines”的问题。 2、 问题场景 客户导入commands.json时报错“Could not obtain system-wide includes and defines”。 3、软硬件环境…

【保姆级教程】DolphinScheduler本地部署与远程访问详细步骤解析

文章目录 前言1. 安装部署DolphinScheduler1.1 启动服务 2. 登录DolphinScheduler界面3. 安装内网穿透工具4. 配置Dolphin Scheduler公网地址5. 固定DolphinScheduler公网地址 前言 本篇教程和大家分享一下DolphinScheduler的安装部署及如何实现公网远程访问&#xff0c;结合内…

海思hi3536c配置内核支持USB摄像头

linux内核版本&#xff1a;linux-3.18.20 配置步骤 进入Device Drivers 选择Multimedia support&#xff0c;并进入 选择Media USB Adapters&#xff0c;并进入 如下图&#xff0c;选择这几项&#xff1a; 保存退出&#xff0c;重新编译内核下载 内核更新后&#xff0c…

家里有宠物想去异味,希喂、米家、范罗士宠物空气净化器哪款去异味好?

宠物的便臭和体臭&#xff0c;其臭味浓度和持续性&#xff0c;相比于正常家居的其他臭味&#xff0c;祛除难度更大&#xff0c;建议就是选使用真正能高效除臭、分解异味分子的化学分解法除臭的宠物空气净化器。比如&#xff1a;光触媒分解除臭的。 不踩坑前置推荐 我从2020年…

docker-compose 部属netcore

一、准备镜像 编写&#xff1a;dockercompose.yml version: "3.4"services: saas.demo.api: image: harbor.net.com/demos/saas.demo.api:latestcontainer_name: saas.demo.apienvironment:- ASPNETCORE_ENVIRONMENTProductionports: - "5001:80" 部属&am…

CTFHUB技能树之HTTP协议——响应包源代码

开启靶场&#xff0c;打开链接&#xff1a; 是个贪吃蛇小游戏&#xff0c;看不出来有什么特别的地方 用burp抓包看看情况&#xff1a; 嗯&#xff1f;点击“开始”没有抓取到报文&#xff0c;先看看网页源代码是什么情况 居然直接给出flag了&#xff0c;不知道这题的意义何在 …

UE4 材质学习笔记06(布料着色器/体积冰着色器)

一.布料着色器 要编写一个着色器首先是看一些参考图片&#xff0c;我们需要找出一些布料特有的特征&#xff0c;下面是一个棉织物&#xff0c;可以看到布料边缘的纤维可以捕捉光线使得边缘看起来更亮 下面是缎子和丝绸的图片&#xff0c;与棉织物有几乎相反的效果&#xff0c;…

Docker 容器 数据卷 使用

目录 常用 命令 什么是数据卷以及特点 如何挂载数据卷 数据卷容器 数据覆盖问题 修改已经建立的数据卷关系 博主wx&#xff1a;yuanlai45_csdn 博主qq&#xff1a;2777137742 想要 深入学习 5GC IMS 等通信知识(加入 51学通信)&#xff0c;或者想要 cpp 方向修改简历&…

Linux——用户/用户组

创建用户组groupadd groupadd 用户组 删除用户组groupdel groupdel 用户组 创建用户useradd useradd 用户名 - g 用户组 useradd 用户名 -d HOME路径 删除用户userdel userdel 用户 userdel -r 用户 &#xff08;删除用户的 HOME 目录&#xff0c;不使用 -r &#xff0…

java项目之纺织品企业财务管理系统源码(springboot+vue+mysql)

风定落花生&#xff0c;歌声逐流水&#xff0c;大家好我是风歌&#xff0c;混迹在java圈的辛苦码农。今天要和大家聊的是一款基于springboot的纺织品企业财务管理系统。项目源码以及部署相关请联系风歌&#xff0c;文末附上联系信息 。 项目简介&#xff1a; 基于spring boot…

西门子网络程序传输,无需开通网络驱动器直接接入底层,支持各类数控 如发那科、三菱 、新代、海德汉、广数、精雕、马扎克等等

有关西门子的程序传输问题&#xff0c;大家一般是通过文件共享、ftp、网络驱动器等方式&#xff0c;其中828D还需要授权开通网络启动器 下面介绍一种方式直接进入西门子Linux底层系统实现和NCK的文件交互功能 软件截图如下 功能表如下 机床程序上载至电脑 电脑程序下传…

什么是 Spring 引导的执行器?

大家好&#xff0c;我是锋哥。今天分享关于【什么是 Spring 引导的执行器&#xff1f;】面试题&#xff1f;希望对大家有帮助&#xff1b; 什么是 Spring 引导的执行器&#xff1f; 1000道 互联网大厂Java工程师 精选面试题-Java资源分享网 Spring Boot 的执行器&#xff08;…