跟着iMeta学做图|circlize绘制环状热图展示细菌功能聚类分析

本文主要是介绍跟着iMeta学做图|circlize绘制环状热图展示细菌功能聚类分析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

原始教程链接 

https://github.com/iMetaScience/iMetaPlot/tree/main/221116circlize

如果你使用本代码,请引用:

Jiao Xi et al. 2022. Microbial community roles and chemical mechanisms in the parasitic development of Orobanche cumana. iMeta https://doi.org/10.1002/imt2.31

写在前面

热图 (Heat map) 可以在微生物组研究中展示展示细菌功能聚类分析的结果,而环状热图是热图的一种表现形式。本期我们挑选2022年6月13日刊登在iMeta上的Microbial community roles and chemical mechanisms in the parasitic development of Orobanche cumana- iMeta | 西农林雁冰/ James M. Tiedje等揭示菌群对寄生植物列当的调控作用,选择文章的Figure 2B进行复现,基于顾祖光博士开发的circlize包,讲解和探讨环形热图的绘制方法,先上原图:

939bea14a50be39019d918a1cc326004.gif

8c564ee0d902770ff9bfb2f0a7c92de5.png

接下来,我们将通过详尽的代码逐步拆解原图,最终实现对原图的复现。

R包检测和安装

01

安装核心R包circlize以及一些功能辅助性R包,并载入所有R包。

# 检查开发者工具devtools,如没有则安装
if (!require("devtools"))install.packages("devtools")
# 加载开发者工具devtools
library(devtools)
# 检查circlize包,没有则通过github安装最新版
if (!require("circlize"))install_github("jokergoo/circlize")
if (!require("tidyverse"))install.packages('tidyverse') 
if (!require("ComplexHeatmap"))install.packages('ComplexHeatmap') 
if (!require("tidyverse"))install.packages('tidyverse') 
# 加载包
library(circlize)
library(tidyverse)
library(ComplexHeatmap)
library(gridBase)

生成测试数据

02

由于没有在补充文件里找到原文相关数据,在这里我们通过生成随机数据来替代。

#生成KEGG数据矩阵(矩阵1)
data1<-matrix(rnorm(670,mean=0.5),nrow=67)
rownames(data1)<-c("K01446","K01971","K01142","K01151","K01246","K00784","K02031","K01644","K02037","K02065","K01448","K01890","K00266","K01725","K00806","K00231","K01737","K00858","K00019","K01715","K01692","K00249","K00023","K00626","K00101","K00803","K01710","K01791","K01176","K00799","K00800","K01667","K01668","K01712","K00053","K01696","K01697","K00108","K00639","K01489","K00226","K01488","K02339","K01428","K01438","K02124","K02275","K01796","K00632","K00648","K00849","K01805","K01685","K00065","K00090","K01619","K01834","K00121","K02182","K02082","K02005","K01266","K01990","K01463","K02217","K01174","K02003")
colnames(data1)<-c(paste('H',seq(1:5),sep = ""),paste("PS",seq(1:5),sep = ""))
#生成EC数据矩阵(矩阵2)
data2<-matrix(rnorm(130,mean=1),nrow = 13)
rownames(data2)<-c("2.1.1.165","1.1.5.-","4.2.99.20","1.1.1.86","1.1.99.1","1.1.2.3","4.2.1.28","4.2.1.82","5.4.1.3","1.13.12.16","5.1.99.4","1.17.4.1","1.8.4.-")
colnames(data2)<-c(paste('H',seq(1:5),sep = ""),paste("PS",seq(1:5),sep = ""))
#生成细菌数据矩阵(矩阵3)
data3<-matrix(rnorm(20,mean=1),nrow = 10)
supdata<-matrix(0,nrow = 10,ncol = 8)
#由于该矩阵为10×2矩阵,需补充10×8全为0的矩阵,使得矩阵123均为m×10的矩阵
data3<-cbind(data3,supdata)
rownames(data3)<-c("Proteobacteria","Actinobacteria","Acidobacteria","Bacteroidetes","Gemmatimonadetes","Chloroflexi","Planctomycetes","Firmicutes","Verrucomicrobia","unidentified_Acidobacteria")
#将三个矩阵按行合并
mat_data<-rbind(data1,data2,data3)
#按行将矩阵反转,这样矩阵3的非零数据会出现在内圈
mat_dataR<-mat_data%>% as.data.frame() %>% rowwise() %>% rev() %>% as.matrix() 
rownames(mat_dataR)<-rownames(mat_data)

6cbed2cca8fbcec940cdd5ed9bd68a9e.png

环形热图预览

03

开始作图,首先画一个最基本的环形热图:

pdf("plot1.pdf",width = 8, height = 6)
#设置热图颜色范围:
colpattern = colorRamp2(c(-1, 0, 1), c("#2574AA", "white", "#ED7B79"))
#设置扇区,这里划分了三个扇区,KEGG,EC和细菌种类。
level_test<-c(rep("KEGG",67),rep("EC",13),rep("SP",10)) %>% factor()#画图
circos.heatmap(mat_dataR, col = colpattern, rownames.side = "outside", cluster = TRUE)
circos.clear()dev.off()

935ed5cd0d448eb356f592d4ddabb600.png

04

添加扇区分化,单元格边框,轨道高度,扇区间间隔:

pdf("plot2.pdf",width = 8, height = 6)
circos.par(gap.after = c(10, 10, 12))
circos.heatmap(mat_dataR, split = level_test, col = colpattern, rownames.side = "outside", cluster = TRUE,cell.lwd=0.8,cell.border="white",track.height = 0.2)
circos.clear()dev.off()

16e6d132267b54781c860773c86b2bd0.png

05

添加矩阵的列名。circos.heatmap()不直接支持矩阵的列名,可以通过自定义panel.fun函数轻松添加:

pdf("plot3.pdf",width = 8, height = 6)
circos.par(gap.after = c(10, 10, 12))
circos.heatmap(mat_dataR, split = level_test, col = colpattern, rownames.side = "outside", cluster = TRUE,cell.lwd=0.8,cell.border="white",track.height = 0.2) 
circos.track(track.index = get.current.track.index(), panel.fun = function(x, y) {if(CELL_META$sector.numeric.index == 1) { # the last sectorcn = colnames(mat_dataR)n = length(cn)circos.text(rep(CELL_META$cell.xlim[2], n) + convert_x(1, "mm"), 1:n - 0.5, cn, cex = 0.3, adj = c(0, 0.5), facing = "inside")}
}, bg.border = NA)circos.track(track.index = get.current.track.index(), panel.fun = function(x, y) {if(CELL_META$sector.numeric.index == 3) { # the last sectorcn = colnames(mat_dataR)n = length(cn)circos.text(rep(CELL_META$cell.xlim[2], n) + convert_x(1, "mm"), 1:n - 0.5, cn, cex = 0.3, adj = c(0, 0.5), facing = "inside")}
}, bg.border = NA)
circos.clear()dev.off()

24f54bb23849419100e586d834eb0d93.png

06

接下来添加连接线,连接线表示位置和位置的对应关系。首先生成数据:

#灰色连接线数据
df_link = data.frame(from_index = sample(nrow(mat_dataR), 30),to_index = sample(nrow(mat_dataR), 30)
)
#红色连接线数据
red_df_link<-data.frame(from_index = c(86,87,82),to_index = c(2,15,36))
#蓝色连接线数据
blue_df_link<-data.frame(from_index = c(84,86,90),to_index = c(72,76,69))

5bed0fb9a9e2a75c7ccdc92953c7bbfa.png

07

接下来开始添加link:

pdf("plot4.pdf",width = 8, height = 6)
circos.par(gap.after = c(10, 10, 12))
circos.heatmap(mat_dataR, split = level_test, col = colpattern, rownames.side = "outside", cluster = TRUE,cell.lwd=0.8,cell.border="white",track.height = 0.2) 
circos.track(track.index = get.current.track.index(), panel.fun = function(x, y) {if(CELL_META$sector.numeric.index == 1) { # the last sectorcn = colnames(mat_dataR)n = length(cn)circos.text(rep(CELL_META$cell.xlim[2], n) + convert_x(1, "mm"), 1:n - 0.5, cn, cex = 0.3, adj = c(0, 0.5), facing = "inside")}
}, bg.border = NA)circos.track(track.index = get.current.track.index(), panel.fun = function(x, y) {if(CELL_META$sector.numeric.index == 3) { # the last sectorcn = colnames(mat_dataR)n = length(cn)circos.text(rep(CELL_META$cell.xlim[2], n) + convert_x(1, "mm"), 1:n - 0.5, cn, cex = 0.3, adj = c(0, 0.5), facing = "inside")}
}, bg.border = NA)for(i in seq_len(nrow(df_link))) {circos.heatmap.link(df_link$from_index[i],df_link$to_index[i],col = "grey")
}for(i in seq_len(nrow(red_df_link))) {circos.heatmap.link(red_df_link$from_index[i],red_df_link$to_index[i],col = "red")
}for(i in seq_len(nrow(blue_df_link))) {circos.heatmap.link(blue_df_link$from_index[i],blue_df_link$to_index[i],col = "blue")
}
circos.clear()dev.off()

13bd8f48fe8ee022b4a586671417a515.png

08

添加图例,circos.heatmap()本身是不支持添加图例的,但我们可以利用gridBase和ComplexHeatmap包添加图例:

pdf("plot5.pdf",width = 8, height = 6)
plot.new()
circle_size = unit(1, "snpc") # snpc unit gives you a square regionpushViewport(viewport(x = 0, y = 0.5, width = circle_size, height = circle_size,just = c("left", "center")))
par(omi = gridOMI(), new = TRUE)
circos.par(gap.after = c(10, 10, 12))
circos.heatmap(mat_dataR, split = level_test, col = colpattern, rownames.side = "outside", cluster = TRUE,cell.lwd=0.8,cell.border="white",track.height = 0.2) 
circos.track(track.index = get.current.track.index(), panel.fun = function(x, y) {if(CELL_META$sector.numeric.index == 1) { # the last sectorcn = colnames(mat_dataR)n = length(cn)circos.text(rep(CELL_META$cell.xlim[2], n) + convert_x(1, "mm"), 1:n - 0.5, cn, cex = 0.3, adj = c(0, 0.5), facing = "inside")}
}, bg.border = NA)circos.track(track.index = get.current.track.index(), panel.fun = function(x, y) {if(CELL_META$sector.numeric.index == 3) { # the last sectorcn = colnames(mat_dataR)n = length(cn)circos.text(rep(CELL_META$cell.xlim[2], n) + convert_x(1, "mm"), 1:n - 0.5, cn, cex = 0.3, adj = c(0, 0.5), facing = "inside")}
}, bg.border = NA)for(i in seq_len(nrow(df_link))) {circos.heatmap.link(df_link$from_index[i],df_link$to_index[i],col = "grey")
}for(i in seq_len(nrow(red_df_link))) {circos.heatmap.link(red_df_link$from_index[i],red_df_link$to_index[i],col = "red")
}for(i in seq_len(nrow(blue_df_link))) {circos.heatmap.link(blue_df_link$from_index[i],blue_df_link$to_index[i],col = "blue")
}
circos.clear()
upViewport()h = dev.size()[2]
lgd = Legend(title = "", col_fun = colpattern)
draw(lgd, x = circle_size, just = "left")dev.off()

49334a7f3203152529d925d27b46e01d.png

09

距离成功只差一步啦,最后用AI进行修图,处理掉不合理的部分。成品图如下:

c3044785366a61daa8ffd47802140514.jpeg

完整代码

# 检查开发者工具devtools,如没有则安装
if (!require("devtools"))install.packages("devtools")
# 加载开发者工具devtools
library(devtools)
# 检查circlize包,没有则通过github安装最新版
if (!require("circlize"))install_github("jokergoo/circlize")
if (!require("tidyverse"))install.packages('tidyverse') 
if (!require("ComplexHeatmap"))install.packages('ComplexHeatmap') 
if (!require("tidyverse"))install.packages('tidyverse') 
# 加载包
library(circlize)
library(tidyverse)
library(ComplexHeatmap)
library(gridBase)#part1 生成数据
set.seed(123)
#生成KEGG数据矩阵(矩阵1)
data1<-matrix(rnorm(670,mean=0.5),nrow=67)
rownames(data1)<-c("K01446","K01971","K01142","K01151","K01246","K00784","K02031","K01644","K02037","K02065","K01448","K01890","K00266","K01725","K00806","K00231","K01737","K00858","K00019","K01715","K01692","K00249","K00023","K00626","K00101","K00803","K01710","K01791","K01176","K00799","K00800","K01667","K01668","K01712","K00053","K01696","K01697","K00108","K00639","K01489","K00226","K01488","K02339","K01428","K01438","K02124","K02275","K01796","K00632","K00648","K00849","K01805","K01685","K00065","K00090","K01619","K01834","K00121","K02182","K02082","K02005","K01266","K01990","K01463","K02217","K01174","K02003")
colnames(data1)<-c(paste('H',seq(1:5),sep = ""),paste("PS",seq(1:5),sep = ""))
#生成EC数据矩阵(矩阵2)
data2<-matrix(rnorm(130,mean=1),nrow = 13)
rownames(data2)<-c("2.1.1.165","1.1.5.-","4.2.99.20","1.1.1.86","1.1.99.1","1.1.2.3","4.2.1.28","4.2.1.82","5.4.1.3","1.13.12.16","5.1.99.4","1.17.4.1","1.8.4.-")
colnames(data2)<-c(paste('H',seq(1:5),sep = ""),paste("PS",seq(1:5),sep = ""))
#生成细菌数据矩阵(矩阵3)
data3<-matrix(rnorm(20,mean=1),nrow = 10)
supdata<-matrix(0,nrow = 10,ncol = 8)
#由于该矩阵为10×2矩阵,需补充10×8全为0的矩阵,使得矩阵123均为m×10的矩阵
data3<-cbind(data3,supdata)
rownames(data3)<-c("Proteobacteria","Actinobacteria","Acidobacteria","Bacteroidetes","Gemmatimonadetes","Chloroflexi","Planctomycetes","Firmicutes","Verrucomicrobia","unidentified_Acidobacteria")
#将三个矩阵按行合并
mat_data<-rbind(data1,data2,data3)
#按行将矩阵反转,这样矩阵3的非零数据会出现在内圈
mat_dataR<-mat_data%>% as.data.frame() %>% rowwise() %>% rev() %>% as.matrix() 
rownames(mat_dataR)<-rownames(mat_data)
#设置热图颜色范围
colpattern = colorRamp2(c(-1, 0, 1), c("#2574AA", "white", "#ED7B79"))
#设置扇区,这里划分了三个扇区,KEGG,EC和细菌种类
level_test<-c(rep("KEGG",67),rep("EC",13),rep("SP",10)) %>% factor()
#level_test<-row.names(mat_dataR) %>% str_match("[k]+") %>% replace_na("EC") %>% factor()
#生成连接线数据
df_link = data.frame(from_index = sample(nrow(mat_dataR), 30),to_index = sample(nrow(mat_dataR), 30)
)
red_df_link<-data.frame(from_index = c(86,87,82),to_index = c(2,15,36))blue_df_link<-data.frame(from_index = c(84,86,90),to_index = c(72,76,69))#开始画图
pdf("Figure 2B.pdf",width = 8, height = 6)
plot.new()
circle_size = unit(1, "snpc") # snpc unit gives you a square regionpushViewport(viewport(x = 0, y = 0.5, width = circle_size, height = circle_size,just = c("left", "center")))
par(omi = gridOMI(), new = TRUE)
circos.par(gap.after = c(10, 10, 12))
circos.heatmap(mat_dataR, split = level_test, col = colpattern, rownames.side = "outside", cluster = TRUE,cell.lwd=0.8,cell.border="white",track.height = 0.2) 
circos.track(track.index = get.current.track.index(), panel.fun = function(x, y) {if(CELL_META$sector.numeric.index == 1) { # the last sectorcn = colnames(mat_dataR)n = length(cn)circos.text(rep(CELL_META$cell.xlim[2], n) + convert_x(1, "mm"), 1:n - 0.5, cn, cex = 0.3, adj = c(0, 0.5), facing = "inside")}
}, bg.border = NA)circos.track(track.index = get.current.track.index(), panel.fun = function(x, y) {if(CELL_META$sector.numeric.index == 3) { # the last sectorcn = colnames(mat_dataR)n = length(cn)circos.text(rep(CELL_META$cell.xlim[2], n) + convert_x(1, "mm"), 1:n - 0.5, cn, cex = 0.3, adj = c(0, 0.5), facing = "inside")}
}, bg.border = NA)for(i in seq_len(nrow(df_link))) {circos.heatmap.link(df_link$from_index[i],df_link$to_index[i],col = "grey")
}for(i in seq_len(nrow(red_df_link))) {circos.heatmap.link(red_df_link$from_index[i],red_df_link$to_index[i],col = "red")
}for(i in seq_len(nrow(blue_df_link))) {circos.heatmap.link(blue_df_link$from_index[i],blue_df_link$to_index[i],col = "blue")
}
circos.clear()
upViewport()h = dev.size()[2]
lgd = Legend(title = "", col_fun = colpattern)
draw(lgd, x = circle_size, just = "left")dev.off()

这篇关于跟着iMeta学做图|circlize绘制环状热图展示细菌功能聚类分析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/675787

相关文章

使用EasyPoi快速导出Word文档功能的实现步骤

《使用EasyPoi快速导出Word文档功能的实现步骤》EasyPoi是一个基于ApachePOI的开源Java工具库,旨在简化Excel和Word文档的操作,本文将详细介绍如何使用EasyPoi快速... 目录一、准备工作1、引入依赖二、准备好一个word模版文件三、编写导出方法的工具类四、在Export

JS纯前端实现浏览器语音播报、朗读功能的完整代码

《JS纯前端实现浏览器语音播报、朗读功能的完整代码》在现代互联网的发展中,语音技术正逐渐成为改变用户体验的重要一环,下面:本文主要介绍JS纯前端实现浏览器语音播报、朗读功能的相关资料,文中通过代码... 目录一、朗读单条文本:① 语音自选参数,按钮控制语音:② 效果图:二、朗读多条文本:① 语音有默认值:②

Python绘制TSP、VRP问题求解结果图全过程

《Python绘制TSP、VRP问题求解结果图全过程》本文介绍用Python绘制TSP和VRP问题的静态与动态结果图,静态图展示路径,动态图通过matplotlib.animation模块实现动画效果... 目录一、静态图二、动态图总结【代码】python绘制TSP、VRP问题求解结果图(包含静态图与动态图

C#实现高性能拍照与水印添加功能完整方案

《C#实现高性能拍照与水印添加功能完整方案》在工业检测、质量追溯等应用场景中,经常需要对产品进行拍照并添加相关信息水印,本文将详细介绍如何使用C#实现一个高性能的拍照和水印添加功能,包含完整的代码实现... 目录1. 概述2. 功能架构设计3. 核心代码实现python3.1 主拍照方法3.2 安全HBIT

录音功能在哪里? 电脑手机等设备打开录音功能的技巧

《录音功能在哪里?电脑手机等设备打开录音功能的技巧》很多时候我们需要使用录音功能,电脑和手机这些常用设备怎么使用录音功能呢?下面我们就来看看详细的教程... 我们在会议讨论、采访记录、课堂学习、灵感创作、法律取证、重要对话时,都可能有录音需求,便于留存关键信息。下面分享一下如何在电脑端和手机端上找到录音功能

Android实现图片浏览功能的示例详解(附带源码)

《Android实现图片浏览功能的示例详解(附带源码)》在许多应用中,都需要展示图片并支持用户进行浏览,本文主要为大家介绍了如何通过Android实现图片浏览功能,感兴趣的小伙伴可以跟随小编一起学习一... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

Python使用FastAPI实现大文件分片上传与断点续传功能

《Python使用FastAPI实现大文件分片上传与断点续传功能》大文件直传常遇到超时、网络抖动失败、失败后只能重传的问题,分片上传+断点续传可以把大文件拆成若干小块逐个上传,并在中断后从已完成分片继... 目录一、接口设计二、服务端实现(FastAPI)2.1 运行环境2.2 目录结构建议2.3 serv

Three.js构建一个 3D 商品展示空间完整实战项目

《Three.js构建一个3D商品展示空间完整实战项目》Three.js是一个强大的JavaScript库,专用于在Web浏览器中创建3D图形,:本文主要介绍Three.js构建一个3D商品展... 目录引言项目核心技术1. 项目架构与资源组织2. 多模型切换、交互热点绑定3. 移动端适配与帧率优化4. 可

Debian 13升级后网络转发等功能异常怎么办? 并非错误而是管理机制变更

《Debian13升级后网络转发等功能异常怎么办?并非错误而是管理机制变更》很多朋友反馈,更新到Debian13后网络转发等功能异常,这并非BUG而是Debian13Trixie调整... 日前 Debian 13 Trixie 发布后已经有众多网友升级到新版本,只不过升级后发现某些功能存在异常,例如网络转

MySQL中查询和展示LONGBLOB类型数据的技巧总结

《MySQL中查询和展示LONGBLOB类型数据的技巧总结》在MySQL中LONGBLOB是一种二进制大对象(BLOB)数据类型,用于存储大量的二进制数据,:本文主要介绍MySQL中查询和展示LO... 目录前言1. 查询 LONGBLOB 数据的大小2. 查询并展示 LONGBLOB 数据2.1 转换为十