跟着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

相关文章

Java中Springboot集成Kafka实现消息发送和接收功能

《Java中Springboot集成Kafka实现消息发送和接收功能》Kafka是一个高吞吐量的分布式发布-订阅消息系统,主要用于处理大规模数据流,它由生产者、消费者、主题、分区和代理等组件构成,Ka... 目录一、Kafka 简介二、Kafka 功能三、POM依赖四、配置文件五、生产者六、消费者一、Kaf

Go语言实现将中文转化为拼音功能

《Go语言实现将中文转化为拼音功能》这篇文章主要为大家详细介绍了Go语言中如何实现将中文转化为拼音功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 有这么一个需求:新用户入职 创建一系列账号比较麻烦,打算通过接口传入姓名进行初始化。想把姓名转化成拼音。因为有些账号即需要中文也需要英

基于WinForm+Halcon实现图像缩放与交互功能

《基于WinForm+Halcon实现图像缩放与交互功能》本文主要讲述在WinForm中结合Halcon实现图像缩放、平移及实时显示灰度值等交互功能,包括初始化窗口的不同方式,以及通过特定事件添加相应... 目录前言初始化窗口添加图像缩放功能添加图像平移功能添加实时显示灰度值功能示例代码总结最后前言本文将

使用Python实现批量访问URL并解析XML响应功能

《使用Python实现批量访问URL并解析XML响应功能》在现代Web开发和数据抓取中,批量访问URL并解析响应内容是一个常见的需求,本文将详细介绍如何使用Python实现批量访问URL并解析XML响... 目录引言1. 背景与需求2. 工具方法实现2.1 单URL访问与解析代码实现代码说明2.2 示例调用

使用Python绘制蛇年春节祝福艺术图

《使用Python绘制蛇年春节祝福艺术图》:本文主要介绍如何使用Python的Matplotlib库绘制一幅富有创意的“蛇年有福”艺术图,这幅图结合了数字,蛇形,花朵等装饰,需要的可以参考下... 目录1. 绘图的基本概念2. 准备工作3. 实现代码解析3.1 设置绘图画布3.2 绘制数字“2025”3.3

使用Python绘制可爱的招财猫

《使用Python绘制可爱的招财猫》招财猫,也被称为“幸运猫”,是一种象征财富和好运的吉祥物,经常出现在亚洲文化的商店、餐厅和家庭中,今天,我将带你用Python和matplotlib库从零开始绘制一... 目录1. 为什么选择用 python 绘制?2. 绘图的基本概念3. 实现代码解析3.1 设置绘图画

最好用的WPF加载动画功能

《最好用的WPF加载动画功能》当开发应用程序时,提供良好的用户体验(UX)是至关重要的,加载动画作为一种有效的沟通工具,它不仅能告知用户系统正在工作,还能够通过视觉上的吸引力来增强整体用户体验,本文给... 目录前言需求分析高级用法综合案例总结最后前言当开发应用程序时,提供良好的用户体验(UX)是至关重要

python实现自动登录12306自动抢票功能

《python实现自动登录12306自动抢票功能》随着互联网技术的发展,越来越多的人选择通过网络平台购票,特别是在中国,12306作为官方火车票预订平台,承担了巨大的访问量,对于热门线路或者节假日出行... 目录一、遇到的问题?二、改进三、进阶–展望总结一、遇到的问题?1.url-正确的表头:就是首先ur

Python绘制土地利用和土地覆盖类型图示例详解

《Python绘制土地利用和土地覆盖类型图示例详解》本文介绍了如何使用Python绘制土地利用和土地覆盖类型图,并提供了详细的代码示例,通过安装所需的库,准备地理数据,使用geopandas和matp... 目录一、所需库的安装二、数据准备三、绘制土地利用和土地覆盖类型图四、代码解释五、其他可视化形式1.

如何评价Ubuntu 24.04 LTS? Ubuntu 24.04 LTS新功能亮点和重要变化

《如何评价Ubuntu24.04LTS?Ubuntu24.04LTS新功能亮点和重要变化》Ubuntu24.04LTS即将发布,带来一系列提升用户体验的显著功能,本文深入探讨了该版本的亮... Ubuntu 24.04 LTS,代号 Noble NumBAT,正式发布下载!如果你在使用 Ubuntu 23.