热图我们号的热图系列已经写的很完善了,也写过其他的热图,随便在公众号检索关键词"热图"就有很多,这里就不再列举了。要是一般的热图设置什么的网上也是一大堆,我们也没有写的必要。这里要写的这个热图主要是为了解决一个问题,就是关于热图的注释。近期关注我们号的小伙伴应该了解,我们最近出的作图函数基本上都是采用点的注释,而很多文章中的热图也是这种形式,可能是有PS,但是我们还是可以使用函数代码实现,所以这里我们写一下。此外,我们也是通过一个视频教程,讲解一下Complexheatmap热图的做法,其实能够解决90%的问题。
视频链接:
这里我们的示例数据是单细胞,其他数据也是一样的,我们只不过是利用单细胞数据构建一个作图的矩阵而已,作图使用的是Heatmap函数。首先构建数据:#加载单细胞数据
load("D:/KS项目/公众号文章/单细胞ATAC-scRNA基因气泡图/sce_test.Rdata")library(Seurat)library(dplyr)DefaultAssay(sce) <- "RNA"Idents(sce) <- "celltype"celltype_markers <- FindAllMarkers(sce, only.pos = TRUE, min.pct = 0.5, logfc.threshold = 0.5)
top30 = celltype_markers %>% group_by(cluster) %>% top_n(n = 30, wt = avg_log2FC)
gene_cell_exp <- AverageExpression(sce, features = top30$gene, group.by = 'celltype', slot = 'data') gene_cell_exp <- as.data.frame(gene_cell_exp$RNA)marker_exp <- t(scale(t(gene_cell_exp),scale = T,center = T))
构建注释:
library(ComplexHeatmap)length(colnames(gene_cell_exp))ha = HeatmapAnnotation("type" = anno_points(rep(0.5,7), which = "column", pch=c(rep(16,7)), size = unit(7, "mm"), axis=F, gp = gpar(col = dittoColors()[1:7]), border=F, ylim=c(0,1)), show_annotation_name = FALSE)
作图:
ht_list <- Heatmap(marker_exp, cluster_rows = F, cluster_columns = F, show_column_names = T, show_row_names = F, column_title = NULL, heatmap_legend_param = list( title=' '),#legend设置 col = colorRampPalette(c('#1A5592','white',"#B83D3D"))(100), row_names_gp = gpar(fontsize = 10), column_names_gp = gpar(fontsize = 10), bottom_annotation = ha,#注释放在底部,如果需要放在顶部,则选择参数top_annotation border = "black")
调整下列的顺序:
ht_list <- Heatmap(marker_exp, cluster_rows = F, cluster_columns = F, show_column_names = T, show_row_names = F, column_title = NULL, heatmap_legend_param = list( title=' '), col = colorRampPalette(c('#1A5592','white',"#B83D3D"))(100), row_names_gp = gpar(fontsize = 10), column_names_gp = gpar(fontsize = 10), bottom_annotation = ha, border = "black", column_order = c("SMC","LY","UEC","SF", "CEC","EC","MAC"))#调整列的顺序,让热图更加美观
这样就完美实现了,觉得分享有用的点个赞、分享下再走呗!