ggcor 的环形热图
感谢老俊俊的大力支持。我们会每日跟新,欢迎您关注老俊俊的生信笔记。
点击上方关注我们
今天学习一下 ggcor 如何绘制环形热图,ggcor 是一款强大的绘制 相关性热图
的 R 包,网上教程很多,大家感兴趣搜索即可。
1、构造数据
这里还是使用之前的方法:
# 构造热图数据
# 写个批量随机生成名字的函数
add_name <- function(n){
sample(LETTERS[1:26],5,replace = T) %>% paste(.,sep = '',collapse = '')
}
# 生成25个名字
my_name <- lapply(1:25, add_name) %>% Reduce('rbind',.)
# 生成25行随机矩阵
mat <- matrix(rnorm(125),ncol = 5)
# 转为数据框
mat <- as.data.frame(mat)
# 增加行名
rownames(mat) <- my_name
# 增加列名
colnames(mat)[1:5] <- paste('S',1:5,sep = '')
# 查看内容
head(mat,3)
S1 S2 S3 S4 S5
DADZX -0.8865726 0.2218578 -2.3218800 -1.4207947 -2.30872404
RSXQH -1.1686315 -1.1954912 -0.3923487 -0.6683483 -0.24387706
SYBZO -0.9267627 -1.0018120 0.8926237 -1.4758484 0.06554279
2、一般样式热图
我们需要使用 cor_tbl
函数把数据转换成 ggcor 可以识别的格式,cluster
参数可以指定为 col、row、all、none、TRUE、FALSE,来显示聚类的方式:
# 转换格式
# 行列不聚类
da <- cor_tbl(mat,cluster = F)
p1 <- quickcor(da,circular = F,
# 基因名位置
axis.y.position = 'right') +
# 单元格边框线颜色
geom_colour(colour = 'black') +
# 自定义填充颜色
scale_fill_gradient2(low = 'blue',mid = 'white',high = 'red') +
# 更改图例名称
guides(fill = guide_colorbar(title = 'log2FC'))
# 行列聚类
da <- cor_tbl(mat,cluster = T)
p2 <- quickcor(da,circular = F,
# 基因名位置
axis.y.position = 'right') +
# 单元格边框线颜色
geom_colour(colour = 'black') +
# 自定义填充颜色
scale_fill_gradient2(low = 'blue',mid = 'white',high = 'red') +
# 更改图例名称
guides(fill = guide_colorbar(title = 'log2FC'))
# 拼图
p1 | p2

我们看看转换格式的数据长啥样:
# 查看转换格式数据
head(da,3)
# A tibble: 3 x 5
.row.names .col.names r .row.id .col.id
<chr> <chr> <dbl> <int> <int>
1 BIFVU S1 -0.341 25 1
2 PPYEX S1 -0.559 24 1
3 IRYIV S1 0.394 23 1
可以看到基因的表达值在 r
这一列,基因名在 .row.names
列,样本名在 .col.names
列,感觉就像是做了一个 宽数据转长数据 的操作。
我们还可以添加 聚类树图 ,使用 anno_col_tree
和 anno_row_tree
函数,注意,添加聚类树时在转换格式函数里的 cluster 参数要对应:
# 添加聚类树
p3 <- quickcor(da,circular = F,
# 基因名位置
axis.y.position = 'right') +
# 单元格边框线颜色
geom_colour(colour = 'black') +
# 自定义填充颜色
scale_fill_gradient2(low = 'blue',mid = 'white',high = 'red') +
# 更改图例名称
guides(fill = guide_colorbar(title = 'log2FC')) +
# 列聚类树,height为树高度
anno_col_tree(height = 0.05) +
# 行聚类树,pos为树的位置
anno_row_tree(pos = 'left')
p3

还可以给聚类树不同的 分枝添加颜色
:
# 给树添加颜色
p4 <- quickcor(da,circular = F,
# 基因名位置
axis.y.position = 'right') +
# 单元格边框线颜色
geom_colour(colour = 'black') +
# 自定义填充颜色
scale_fill_gradient2(low = 'blue',mid = 'white',high = 'red') +
# 更改图例名称
guides(fill = guide_colorbar(title = 'log2FC')) +
# 列聚类树,height为树高度
anno_col_tree(height = 0.05,
# 树分支颜色
bcols = c('#FB9300','#0A81AB')) +
# 行聚类树,pos为树的位置
anno_row_tree(pos = 'left',
# 树分支颜色
bcols = rainbow(5))
p4

添加 行列注释
:
# 添加行列注释
p5 <- quickcor(da,circular = F,
# 基因名位置
axis.y.position = 'right') +
# 单元格边框线颜色
geom_colour(colour = 'black') +
# 自定义填充颜色
scale_fill_gradient2(low = 'blue',mid = 'white',high = 'red') +
# 更改图例名称
guides(fill = guide_colorbar(title = 'log2FC')) +
# 列注释
anno_hc_bar(k = 2,fill = c('#FB9300','#0A81AB'),
pos = 'top') +
# 列聚类树,height为树高度
anno_col_tree(height = 0.05,
# 树分支颜色
bcols = c('#0A81AB','#FB9300')) +
# 行聚类树,pos为树的位置
anno_row_tree(pos = 'left',
# 树分支颜色
bcols = rainbow(5)) +
# 行注释
anno_hc_bar(k = 5,fill = rainbow(5),
pos = 'left')
p5

添加多层注释:
# 添加多层注释
library(circlize)
p6 <- quickcor(da,circular = F,
# 基因名位置
axis.y.position = 'left') +
# 单元格边框线颜色
geom_colour(colour = 'black') +
# 自定义填充颜色
scale_fill_gradient2(low = 'blue',mid = 'white',high = 'red') +
# 更改图例名称
guides(fill = guide_colorbar(title = 'log2FC')) +
# 列注释
anno_hc_bar(k = 5,fill = rand_color(5),pos = 'bottom') +
anno_hc_bar(k = 5,fill = rand_color(5),pos = 'top') +
anno_hc_bar(k = 3,fill = rand_color(3),pos = 'bottom') +
anno_row_tree(bcols = rainbow(5),pos = 'left') +
# 行注释
anno_hc_bar(k = 5,fill = rainbow(5),pos = 'right') +
anno_hc_bar(k = 8,fill = rand_color(8),pos = 'right',width = 0.7) +
anno_hc_bar(k = 20,fill = rand_color(20),pos = 'right',width = 0.7)
p6

没有图例,弄着玩就行,要绘制热图最好用 pheatmap
和 complexheatmap
专业的包吧。
3、环形热图
有了上面一些基础,我们试着画一下环形热图:
# 环形热图
da <- cor_tbl(mat,cluster = T)
p7 <- quickcor(da,circular = T,cluster = T,
open = 45, # 缺口大小
# 内圈外圈比例
outer = 0.5,inner = 1.5) +
# 单元格边框线颜色
geom_colour(colour = 'black') +
# 自定义填充颜色
scale_fill_gradient2(low = 'blue',mid = 'white',high = 'red') +
# 更改图例名称
guides(fill = guide_colorbar(title = 'log2FC')) +
# 添加聚类树
anno_col_tree() +
anno_row_tree() +
# 基因名
set_p_yaxis() +
# 样本名
set_p_xaxis()
给树和标签添加一些颜色:
p8 <- quickcor(da,circular = T,cluster = T,grid.colour = 'white',
open = 45, # 缺口大小
# 内圈外圈比例
outer = 0.2,inner = 2) +
# 单元格边框线颜色
geom_colour(colour = 'white') +
# 自定义填充颜色
scale_fill_gradient2(low = 'blue',mid = 'white',high = 'red') +
# 更改图例名称
guides(fill = guide_colorbar(title = 'log2FC')) +
# 添加聚类树
anno_col_tree(bcols = rand_color(5)) +
anno_row_tree(bcols = rand_color(10)) +
# 基因名
set_p_yaxis(bcols = rand_color(10)) +
# 样本名
set_p_xaxis(bcols = rand_color(5))
p7 + p8

同样也可给环形热图添加注释:
# 环形热图行列注释
quickcor(da,circular = T,cluster = T,grid.colour = 'white',
open = 45, # 缺口大小
# 内圈外圈比例
outer = 0.5,inner = 1) +
# 单元格边框线颜色
geom_colour(colour = 'white') +
# 自定义填充颜色
scale_fill_gradient2(low = 'blue',mid = 'white',high = 'red') +
# 更改图例名称
guides(fill = guide_colorbar(title = 'log2FC')) +
# 列注释
anno_hc_bar(k = 2,fill = rand_color(2),pos = 'top',height = 0.3) +
anno_hc_bar(k = 3,fill = rand_color(3),pos = 'top',height = 0.5) +
anno_hc_bar(k = 5,fill = rand_color(5),pos = 'top',height = 0.2) +
# 添加聚类树
anno_col_tree(bcols = rand_color(5),height = 0.15) +
anno_hc_bar(k = 15,fill = rand_color(15),pos = 'left',width = 0.5) +
anno_hc_bar(k = 10,fill = rand_color(10),pos = 'left',width = 0.5) +
anno_row_tree(bcols = rand_color(8)) +
# 样本名
set_p_xaxis(bcols = rand_color(5)) +
# 基因名
set_p_yaxis()

还使用 anno_row_heat
可以在外圈添加热图:
# 添加热图
p9 <- quickcor(da,circular = T,cluster = T,grid.colour = 'white',
open = 45, # 缺口大小
# 内圈外圈比例
outer = 2,inner = 1) +
# 单元格边框线颜色
geom_colour(colour = 'white') +
# 自定义填充颜色
scale_fill_gradient2(low = 'blue',mid = 'white',high = 'red',name = 'log2FC') +
# 更改图例名称
# guides(fill = guide_colorbar(title = 'log2FC')) +
# 列注释
anno_hc_bar(k = 2,fill = rand_color(2),pos = 'top',height = 0.3) +
anno_hc_bar(k = 3,fill = rand_color(3),pos = 'top',height = 0.5) +
anno_hc_bar(k = 5,fill = rand_color(5),pos = 'top',height = 0.2) +
# 添加聚类树
anno_col_tree(bcols = rand_color(5),height = 0.15) +
anno_hc_bar(k = 15,fill = rand_color(15),pos = 'left',width = 0.5) +
anno_hc_bar(k = 10,fill = rand_color(10),pos = 'left',width = 0.5) +
anno_row_tree(bcols = rand_color(8)) +
# 样本名
set_p_xaxis(bcols = rand_color(5))
p9 + anno_row_heat(da,aes(fill = r),width = 0.8,
geom = 'tile') +
scale_fill_gradient2(low = 'green',mid = 'white',high = 'red',name = 'expression') +
# 基因名
set_p_yaxis()

添加点:
# 添加点
p9 + anno_row_heat(da,aes(color = r),width = 0.8,
geom = 'point') +
scale_color_gradient2(low = '#F9B208',mid = 'white',high = '#F55C47',name = 'expression') +
# 基因名
set_p_yaxis()

欢迎加入生信交流群。加我微信我也拉你进 微信群聊 老俊俊生信交流群
哦。
群二维码:

老俊俊微信:
知识星球:
所以今天你学习了吗?
欢迎小伙伴留言评论!
今天的分享就到这里了,敬请期待下一篇!
最后欢迎大家分享转发,您的点赞是对我的鼓励和肯定!
如果觉得对您帮助很大,赏杯快乐水喝喝吧!
往期回顾
◀ComplexHeatmap 之 Legends 续(二)
◀ComplexHeatmap 之 Legends 续(一)
◀ComplexHeatmap 之 Heatmap List 续(二)
◀ComplexHeatmap 之 Heatmap List 续(一)
◀ComplexHeatmap 之 Heatmap List
◀ComplexHeatmap 之 Heatmap Annotations 续(三)
请关注“恒诺新知”微信公众号,感谢“R语言“,”数据那些事儿“,”老俊俊的生信笔记“,”冷🈚️思“,“珞珈R”,“生信星球”的支持!