单细胞celltype颜色、顺序设置

2024-03-27  本文已影响0人  KS科研分享与服务

今天我们这一期要说的内容其实很简单,之前我们在写其他内容的时候或多或少的提到过,但是奈何问的人实在很多,次数也很多,所以索性这里出个帖子,将这个问题直接综合起来讲一下。这个内容就是我们用seurat作图的时候,例如Dimplot做降维图的时候,如何指定cluster的颜色。用Vlnplot或者Dotplot作图的时候如何设置顺序。那么最后还有一个小问题就是seurat V5 object的使用,其实seurat的更新并不是很可怕,遇到那里有错,解决就可以了!

一、指定降维图中cluter的颜色
我们使用Dimplot做将降维图的时候,每个cluster或者celltype的颜色是利用cols参数设置的,那么很多小伙伴就提出问题,我修改颜色的时候如何指定呢。很简单:

#==========================================================================
#              1、cluster/celltype指定颜色设置
#==========================================================================

library(Seurat)

#指定cluster/celltype的颜色


Idents(uterus) <- "seurat_clusters"

clusterCols <- c("#843C39", "#8C6D31", "#E6550D", "#3182BD", "#54990F",
                 "#BD9E39", "#E7BA52", "#31A354", "#E41A1C", "#6BAED6",
                 "#9ECAE1", "#AD494A", "#A1D99B", "#C7E9C0", "#99600F",
                 "#C3BC3F", "#D6616B", "#FF7F00", "#1B9E77", "#FDAE6B", 
                 "#66A61E", "#F1788D", "#E6550D", "#E7969C")

names(clusterCols) <- c(0:24)
DimPlot(uterus, group.by='seurat_clusters', cols=clusterCols, pt.size=1, raster=F, label = T)+NoLegend()



unique(uterus$celltype)
# [1] "Smooth muscle cells"         "Lymphocytes"                
# [3] "Unciliated epithelial cells" "Stromal fibroblasts"        
# [5] "Ciliated epithelial cells"   "Endothelial cells"          
# [7] "Macrophages" 
Idents(uterus) <- "celltype"
cols <- c("#843C39", "#E6550D", "#3182BD", "#54990F", "#FF7F00", "#1B9E77", "#FDAE6B")
#将需要的颜色与cell type对应
names(cols) <- c("Lymphocytes",
                    "Stromal fibroblasts",
                    "Unciliated epithelial cells",
                    "Endothelial cells",
                    "Smooth muscle cells",
                    "Ciliated epithelial cells",
                    "Macrophages" )                
DimPlot(uterus, group.by='celltype', cols=cols, pt.size=1, raster=F, label = T)+NoLegend()

二、作图顺序的指定
这个问题真的说过无数次了,设置顺序其实就是factor就可以了!

#==========================================================================
#              2、seurat作图修改celltype或者cluster顺序
#==========================================================================
library(ggplot2)
#比如我做一个小提琴图
p1 = VlnPlot(uterus, features = "CD74", pt.size = 0, cols = cols)+theme(axis.title = element_blank(),
                                                           axis.text.x = element_text(angle = 90))+NoLegend()


Idents(uterus) <- factor(Idents(uterus), levels =  c("Lymphocytes",
                                                       "Stromal fibroblasts",
                                                       "Unciliated epithelial cells",
                                                       "Endothelial cells",
                                                       "Smooth muscle cells",
                                                       "Ciliated epithelial cells",
                                                       "Macrophages" ) )
p2 = VlnPlot(uterus, features = "CD74", pt.size = 0, cols = cols)+theme(axis.title = element_blank(),
                                                           axis.text.x = element_text(angle = 90))+NoLegend()

p1|p2
#同样的设置后,Dotplot也会随着自定义顺序显示
markers <- c("ACTA2", "RGS5", #smooth muscle cells---7, 16
             "MS4A6A", "CD68","LYZ",#macrophages---13
             "CCL5", "STK17B","PTPRC",#lymphocytes---0,3,4,5,6,14,15,17,23,18,19
             "DCN", "COL6A3", "LUM",#stromal fibroblasts---2,20
             "PECAM1","PCDH17", "VWF",#endothelial cells---8,11,22
             "EPCAM", "CDH1",#(unciliated)epithelial cells---1,9,21
             "FOXJ1","CDHR3","DYDC2")#(ciliated)epithelial cells---10,12
DotPlot(uterus, features = markers)+theme_bw()+theme(axis.title = element_blank(),
                                                     axis.text.x = element_text(angle = 90))

#对于split的
p3 = DimPlot(uterus, split.by = "orig.ident")
#可以看到celltype legend的排列是按照我们设置的顺序。但是我们需要设置分组的顺序
uterus$orig.ident <- factor(uterus$orig.ident, levels = c("HC","EEC","AEH"))
p4 = DimPlot(uterus, split.by = "orig.ident")

p3/p4

更多请参考:https://mp.weixin.qq.com/s/7KeJi0mhCmHFsAucGWnWwQ
上一篇下一篇

猜你喜欢

热点阅读