在线客服
客服热线
400-664-9912
客服组:
阿趣客服
客服组:
在线客服
QQ:
Amy
服务时间:
8:00 - 24:00

关注官方微信平台

电话:400-664-9912

QQ:2850277929

邮箱:marketing@biotree.cn

地址:上海市嘉定区新培路51号焦点梦想园B栋5楼

Copyright 2012-2020 SHANGHAI BIOTREE BIOTECH CO., LTD. All rights reserved.上海阿趣生物科技有限公司. 版权所有 沪ICP备17019578号-1

实用干货 | 轻松绘制Circos图——环形热图

分类:
阿趣动态
发布时间:
2021/11/22 14:27
浏览量

不会代码的朋友也能画漂亮的热图,不要走!!!

 

圈图在生信分析可视化中十分常见,而circos软件的使用较为复杂,在这里分享R包「circlize」[1]的circos.heatmap函数,用于绘制环形热图。

 

该包的详细说明文件见:https://jokergoo.github.io/circlize_book/book/introduction.html#principle-of-design

无脑复制代码绘图

```

##代码1(标准化圈图)

##安装包

install.packages("dendextend","circlize","openxlsx")

##加载包

library(circlize)

library(dendextend)

library(openxlsx)

##读入数据

mat1 <- read.xlsx("C:\\Users\\user\\Desktop\\工作空间\\热图\\data.xlsx")##读入xlsx表

#((C:\\Users\\xyf\\Desktop\user\工作空间\\热图\\data.xlsx)替换为你的数据的路径)

row.names(mat1)<-mat1[,1]#修改行名

mat1<-mat1[,-1]##删除第一列,使之变为数字矩阵,绘图的数据要求为矩阵(也就是单一类型的数据矩阵,这里全为数字)

##绘图

col_fun1 = colorRamp2(c(-2, 0, 2), c("blue", "white", "red"))##设置热图颜色

column_od = hclust(dist(t(mat1)))$order #对列聚类

circos.par(gap.after = c(10))##为添加列名留出空间

circos.heatmap(mat1[, column_od], ##将列聚类后重新排序的矩阵

               col = col_fun1, ##设置颜色

               dend.side = "inside",##树状图在圈内

               rownames.side = "outside",##行名在圈外

               dend.track.height = 0.2,

               dend.callback = function(dend, m, si) {

                 # when k = 1, it renders one same color for the whole dendrogram

                 color_branches(dend, k = 4, col = 2:5)##对树状图进行着色

               }

)

circos.track(track.index = 2, ##将列名添加在第二个轨道(就是热图所在的环形轨道)

             panel.fun = function(x, y) {

               if(CELL_META$sector.numeric.index == 1) { # the last sector

                 cn = colnames(mat1[, column_od])##取得列名

                 n = length(cn)

                 circos.text(rep(CELL_META$cell.xlim[2], n) + convert_x(1, "mm"), ##x轴坐标

                             1:n - convert_y(0.5, "mm"), ##y轴坐标

                             cn, ##输入要展示的列名

                             cex = 0.25, ##列名的大小

                             adj = c(0, 0.5),

                             facing = "inside")

               }

             }, bg.border = NA)

 

circos.clear()

```

细致讲解

第一步:准备输入数据。

一般用热图展示基因在样本中的表达变化,代码中data.xlsx为输入文件,格式见图1。

 图1

 

第二步:安装软件(R和Rstudio)和包("dendextend","circlize","openxlsx")。

从这两个网址https://cran.r-project.org/bin/windows/base/R-4.1.2-win.exe和https://download1.rstudio.org/desktop/windows/RStudio-2021.09.1-372.exe(复制到浏览器即可打开)下载软件,直接双击安装,所有选项默认。

用下面的代码2安装三个包。

```

##代码2

 

##安装分析包

install.packages("dendextend","circlize","openxlsx")

##加载包

library(circlize)

library(dendextend)

library(openxlsx)

```

 

第三步:代码参数调整。

1> 修改颜色,使用代码3,结果见图2。颜色编号("#26B9CB", "#FFFFFF", "#B72865")可以在该网址:https://htmlcolorcodes.com/ 随意选择。

```

##代码3

col_fun1 = colorRamp2(c(-2, 0, 2), c("#26B9CB", "#FFFFFF", "#B72865"))##设置热图颜色

column_od = hclust(dist(t(mat1)))$order #对列聚类

circos.par(gap.after = c(10))##为添加列名留出空间

circos.heatmap(mat1[, column_od], ##将列聚类后重新排序的矩阵

               col = col_fun1, ##设置颜色

               dend.side = "inside",##树状图在圈内

               rownames.side = "outside",##行名在圈外

               dend.track.height = 0.2,

               dend.callback = function(dend, m, si) {

                 # when k = 1, it renders one same color for the whole dendrogram

                 color_branches(dend, k = 4, col = 2:5)##对树状图进行着色

               }

)

circos.track(track.index = 2, ##将列名添加在第二个轨道(就是热图所在的环形轨道)

             panel.fun = function(x, y) {

               if(CELL_META$sector.numeric.index == 1) { # the last sector

                 cn = colnames(mat1[, column_od])##取得列名

                 n = length(cn)

                 circos.text(rep(CELL_META$cell.xlim[2], n) + convert_x(1, "mm"), ##x轴坐标

                             1:n - convert_y(0.5, "mm"), ##y轴坐标

                             cn, ##输入要展示的列名

                             cex = 0.25, ##列名的大小

                             adj = c(0, 0.5),

                             facing = "inside")

               }

             }, bg.border = NA)

 

circos.clear()

```

图2

 

2> 修改文字的大小(rownames.cex)。字体(rownames.font)和颜色(rownames.col),使用代码4,结果见图3。

```

##代码4

col_fun1 = colorRamp2(c(-2, 0, 2), c("#26B9CB", "#FFFFFF", "#B72865"))##设置热图颜色

column_od = hclust(dist(t(mat1)))$order #对列聚类

circos.par(gap.after = c(15))##为添加列名留出空间

circos.heatmap(mat1[, column_od], ##将列聚类后重新排序的矩阵

               col = col_fun1, ##设置颜色

               dend.side = "inside",##树状图在圈内

               rownames.side = "outside",##行名在圈外

               rownames.col = 1:nrow(mat1) %% 10 + 1,##行名添加颜色

               rownames.cex = runif(nrow(mat1), min = 0.3, max = 2),##行名的大小

               rownames.font = 1:nrow(mat1) %% 4 + 1,##行名字体

               dend.track.height = 0.2,

               dend.callback = function(dend, m, si) {

                 # when k = 1, it renders one same color for the whole dendrogram

                 color_branches(dend, k = 4, col = 2:5)##对树状图进行着色

               }

)

circos.track(track.index = 2, ##将列名添加在第二个轨道(就是热图所在的环形轨道)

             panel.fun = function(x, y) {

               if(CELL_META$sector.numeric.index == 1) { # the last sector

                 cn = colnames(mat1[, column_od])##取得列名

                 n = length(cn)

                 circos.text(rep(CELL_META$cell.xlim[2], n) + convert_x(1, "mm"), ##x轴坐标

                             1:n - convert_y(0.5, "mm"), ##y轴坐标

                             cn, ##输入要展示的列名

                             cex = 0.25, ##列名的大小

                             adj = c(0, 0.5),

                             facing = "inside")

               }

             }, bg.border = NA)

 

circos.clear()

```

图3

 参考文献

[1] Gu, Z. (2014) circlize implements and enhances circular visualization in R. Bioinformatics. DOI: 10.1093/bioinformatics/btu393.

>
>
>
实用干货 | 轻松绘制Circos图——环形热图