单细胞绘图系列:
1. 使用DoHeatmap
绘制Seurat自带热图
library(Seurat)
library(dplyr)
pbmc <- readRDS("pbmc.rds") #导入注释好的演示数据集
pbmc.markers <- FindAllMarkers(pbmc,only.pos = T,min.pct = 0.1,logfc.threshold = 0.25)
top10 <- pbmc.markers%>%group_by(cluster)%>%top_n(n=10,wt=avg_log2FC)
DoHeatmap(pbmc,features = top10$gene)+NoLegend()
##DoHeatmap输入的的是scale.data矩阵
稍作优化(调整热图颜色+调整细胞类型标签颜色,最好与UMAP图一致)
DoHeatmap(pbmc,
features = as.character(unique(top10$gene)),
group.by = "cell_type",
assay = "RNA",
group.colors = c("#C77CFF","#7CAE00","#00BFC4","#F8766D","#AB82FF","#90EE90","#00CD00","#008B8B"))+ #设置组别颜色
scale_fill_gradientn(colors = c("navy","white","firebrick3"))
scale_fill_gradientn()系列函数用法见:ggplot2点图
更改横轴顺序(根据实际需要)
pbmc$cell_type <- factor(x = pbmc$cell_type, levels = c('Naive CD4 T','Memory CD4 T','CD8 T','CD14+ Mono','FCGR3A+ Mono','B','NK','DC','Platelet'))
DoHeatmap(pbmc,
features = as.character(unique(top10$gene)),
group.by = "cell_type",
assay = "RNA",
group.colors = c("#C77CFF","#7CAE00","#00BFC4","#F8766D","#AB82FF","#90EE90","#00CD00","#008B8B","#FFA500"))+ #设置组别颜色
scale_fill_gradientn(colors = c("navy","white","firebrick3"))#设置热图颜色
2. 使用ComplexHeatmap
绘制带特定基因的热图
ComplexHeatmap
优点:功能非常强大,支持一张热图中分组分别聚类(control之间聚类,treatment之间聚类)
缺点:参数基本上只适用于这一个包
参考:https://jokergoo.github.io/ComplexHeatmap-reference/book/
library(ComplexHeatmap)
##提取标准化表达矩阵
#⚠️提取scale.data矩阵的时候一定要注意,做ScaleData()的时候一定是scale了所有的基因,而不是默认的2000个基因
mat <- GetAssayData(pbmc,slot = 'scale.data')
##获得基因和细胞聚类信息
gene_features <- top10
cluster_info <- sort(pbmc$cell_type)
##筛选矩阵
mat <- as.matrix(mat[top10$gene,names(cluster_info)])
##输入想在图上展示出来的marker基因,获得基因在热图中的位置信息
gene <- c('CD3E','CD8B','S100A9','CD14','LYZ',"CD79B","GNLY","GZMB","PF4")
gene_pos <- which(rownames(mat)%in%gene)
row_anno <- rowAnnotation(gene=anno_mark(at=gene_pos,labels = gene))
##画个热图看看
Heatmap(mat,
cluster_rows = FALSE,
cluster_columns = FALSE,
show_column_names = FALSE,
show_row_names = FALSE,
column_split = cluster_info,
right_annotation = row_anno)
做一下美化,调整一下颜色
##设置列标签的颜色,最好和umap/tsne细胞群的颜色一一对应
col <- c('plum','coral1','bisque','gold2','hotpink3','lightsalmon3','rosybrown2','lightcyan2','thistle3')
names(col) <- levels(cluster_info)
top_anno <- HeatmapAnnotation(cluster=anno_block(gp=gpar(fill=col),
labels = levels(cluster_info),
labels_gp = gpar(cex=0.5,col='white')))
##给热图改个好看的颜色
library(circlize)
col_fun = colorRamp2(c(-2, 1, 4), c("#377EB8", "white", "#E41A1C"))
#col_fun2 = colorRamp2(c(-2, 1, 4), c("#92b7d1", "white", "#d71e22"))
Heatmap(mat,
cluster_rows = FALSE,
cluster_columns = FALSE,
show_column_names = FALSE,
show_row_names = FALSE,
column_split = cluster_info,
top_annotation = top_anno, #在热图边上增加注释
column_title = NULL,
right_annotation = row_anno,
heatmap_legend_param = list(
title='Expression',
title_position='leftcenter-rot'),
col = col_fun)
完成~