用于绘制热图的R包有很多,类似大家熟悉的heatmap、pheatmap、ComplexHeatmap包,从基础热图到稍复杂的个 化热图基本靠上述这三个基本都能拿捏,相关的教程可戳我们往期的推文。
今天强烈安利一个基于ggplot2版的热图绘制R包ggheatmap,相比ComplexHeatmap包来说,使用起来不能更简单,还可结合ggplot2的丰富主题参数进行美化,赶紧学起来!
本期内容一览:
1. 基础热图绘制
2. 为热图添加分组信息
3. 为热图添加指定基因标签
4. 主题美化
#相关R包下载与载入:
devtools::install_github( "XiaoLuo-boy/ggheatmap")
library(ggheatmap)
library(tidyr)
#读入本地热图数据:
df- read.csv('ggheatmap_testdt.csv',header = T,row.names = 1)
df[1:6,1:6]
一、基础热图绘制
p- ggheatmap(
df,
cluster_rows= T, #是否对行聚类
cluster_cols= T, #是否对列聚类
tree_height_rows= 0.28, #行聚类树高度
tree_height_cols= 0.1, #列聚类树高度
scale= "row"#选择对row/column/none进行归一化
)
展开全文
p
为了适应聚类树的粗细,导出尺寸设置为12×12(inches),可以看到字号有些过小了,我们留到最后通过主题一次 改完,这里先过掉。
二、指定基因标签的热图绘制
#设定目标基因集:
mark- rownames(df)[1:10]
mark
p1- ggheatmap(
df,
cluster_rows= T,
cluster_cols= T,
tree_height_rows= 0.28,
tree_height_cols= 0.1,
scale= "row",
text_show_rows= mark #显示指定的基因标签
)
p1
三、指定分组信息的热图绘制
#创建分组数据框:
group- data.frame(row.names = colnames(df),
Group= rep(c('TESA','TESB','TESC','TESD'), each = 3),
Risk_Level= rep(c('Low','High'), each = 6)) #四个分组
head(group)
##添加分组颜 :
group_col- c( "#f3533b", "#fa9f42", "#8cd77a", "#5bcec9")
names(group_col) - c('TESA','TESB','TESC','TESD')
group_col2- c( "#bfb2d5", "#f1937f")
names(group_col2) - c('Low','High')
group_col
group_col2
col- list(Group = group_col,
Risk_Level= group_col2) #转换为list,和分组进行对应
col
#绘制热图:
p2- ggheatmap(
df,
cluster_rows= T,
cluster_cols= T,
tree_height_rows= 0.28,
tree_height_cols= 0.1,
scale= "row",
text_show_rows= mark,
annotation_cols= group, #为列添加分组
annotation_color= col #分组颜
)
p2
四、热图美化
p3- ggheatmap(
df,
cluster_rows= T,
cluster_cols= T,
tree_height_rows= 0.28,
tree_height_cols= 0.1,
scale= "row",
text_show_rows= mark,
annotation_cols= group,
annotation_color= col,
shape= NULL, #还可以选择"square", "circle" and "triangle"三种格子样式,默认无
color= colorRampPalette(c( "#e74a32", "white", "#06a7cd"))(100),
border= c('white'), #格子描边颜
cluster_num= c(1,4), #行列聚类树的颜 数量
tree_color_rows= c( "black"), #行聚类树颜
tree_color_cols= c( "#f3533b", "#fa9f42", "#5bcec9", "#8cd77a") #列聚类树颜
)
p3
#显示热图中的组件:
ggheatmap_plotlist(p3)
除聚类树之外的其它组件,在本案例中也就是plotlist1-plotlist3,我们可以结合ggplot2的theme进行主题样式的自定义。
#主题美化:ggheatmap_theme结合theme【来自ggplot2】
p3%%
ggheatmap_theme(1:3, #对plotlist1-plotlist3进行主题美化
theme= list(
theme(axis.text.x = element_text(angle = 90, size = 16),
axis.text.y = element_text(colour = "black", size = 16),
legend.title = element_text(size = 18, face = 'bold'),
legend.text = element_text(size = 14)), #plotlist1组件主题修改
theme(legend.title = element_text(size = 18, face = 'bold'),
legend.text = element_text(size = 14)), #plotlist2组件主题修改
theme(legend.title = element_text(size = 18, face = 'bold'),
legend.text = element_text(size = 14)) #plotlist3组件主题修改
))
#还原为默认配 ,并显示全部基因标签:
p4- ggheatmap(
df,
cluster_rows= T,
cluster_cols= T,
tree_height_rows= 0.28,
tree_height_cols= 0.1,
scale= "row",
annotation_cols= group,
annotation_color= col,
border= c('white')
)
p4%%
ggheatmap_theme(1:3,
theme= list(
theme(axis.text.x = element_text(angle = 90, size = 16),
axis.text.y = element_text(colour = "black", size = 16),
legend.title = element_text(size = 18, face = 'bold'),
legend.text = element_text(size = 14)),
theme(legend.title = element_text(size = 18, face = 'bold'),
legend.text = element_text(size = 14)),
theme(legend.title = element_text(size = 18, face = 'bold'),
legend.text = element_text(size = 14))
))
给平时用惯了ggplot2绘图的朋友们安利这个R包,使用起来会非常顺手,效果也狠不错!好啦,今天的分享就到这里!
参考资料:
传奇 s github复古传奇 /XiaoLuo-boy/ggheatmap
*未经许可,不得以任何方式复制或抄袭本篇文章之部分或全部内容。版权所有,侵权必究。
基迪奥生物|专业定制测序服务
联系方式:020-39341079;service@genedenovo复古传奇