看到了一篇Nature的UMAP图,展示的是单细胞,但是特点是标记了celltype的同时指示了marker基因。显然这个处理很有特色,我们可以用代码指示,但是很难一次性达到这个效果,最后借助AI可能会更加方便,一次性完成这个事情,关于AI的操作,我们是直接整了个视频,希望对你有用!
(reference:IL-1β+ macrophages fuel pathogenic inflammation in pancreatic cancer)
普通的UMAP是这样的:
setwd('D:\\KS项目\\公众号文章\\复现nature单细胞UMAP降维图修饰')
library(Seurat)
library(ggplot2)
library(ggrastr)
library(tidydr)
library(dplyr)
library(ggrepel)
# load("C:/Users/tq199/Downloads/adj_scRNA.RData")
adj_scRNA <- subset(adj_scRNA, celltype=="Other", invert=T)
DimPlot(adj_scRNA, label = T)
image.png
之后我们提取坐标,用ggplot修饰一下:
df <- adj_scRNA@reductions$umap@cell.embeddings%>%
as.data.frame() %>%
cbind(cell_type = adj_scRNA@meta.data$celltype)
label <- df %>%group_by(cell_type) %>%
summarise(UMAP_1 = median(UMAP_1),
UMAP_2 = median(UMAP_2))%>%
as.data.frame()
rownames(label) <- label$cell_type
label$number <- seq(1:9)
cols= c('#7F3C8D' ,'#11A579', '#3969AC',
'#E73F74', '#80BA5A', '#E68310',
'#008695', '#CF1C90', '#f97b72')
#ggplot作图
p = ggplot()+
geom_point_rast(data=df, aes(x= UMAP_1 , y = UMAP_2 ,color = cell_type),size = 1,shape=16) +
scale_color_manual(values = alpha(cols,0.3))+ #设置下透明度
theme_classic()+
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.ticks = element_blank(),
axis.line = element_blank(),
axis.title = element_blank(),
axis.text = element_blank(),
legend.position = 'none')+
geom_point(data = label, aes(x= UMAP_1 , y = UMAP_2), size=6, color='white', alpha=0.9)+
geom_point(data = label, aes(x= UMAP_1 , y = UMAP_2), size=6, color='black', shape=21)+
geom_text(data = label,
mapping = aes(x= UMAP_1 , y = UMAP_2, label = number),
color='black')
image.png
然后标记基因即可:
marker_gene <- FindAllMarkers(adj_scRNA, only.pos = T,
logfc.threshold = 0.8, min.pct = 0.8)
#每种celltype挑选5个展示
marker_genes <- marker_gene %>%
group_by(cluster) %>%
top_n(n = 5, wt = avg_log2FC)
write.csv(marker_genes, file = 'marker_genes.csv')
p + geom_label_repel(
data=label[which(label$UMAP_1>0 &label$UMAP_2>0),],
aes(x= UMAP_1, y = UMAP_2,label=labels),
size=3,
nudge_x = 4,
box.padding = 0.5,
nudge_y = 8,
segment.curvature = -0.1,
segment.ncp = 3,
segment.angle = 20,
direction = "y", hjust = "left")+
geom_label_repel(
data=label[which(label$UMAP_1>0 &label$UMAP_2<0),],
aes(x= UMAP_1, y = UMAP_2,label=labels),
size=3,
nudge_x = 4,
box.padding = 0.5,
nudge_y = -8,
segment.curvature = -0.1,
segment.ncp = 3,
segment.angle = 20,
direction = "y", hjust = "left")+
geom_label_repel(
data=label[which(label$UMAP_1<0 &label$UMAP_2>3),],
aes(x= UMAP_1, y = UMAP_2,label=labels),
size=3,
nudge_x = -6,
box.padding = 0.5,
nudge_y = 15,
segment.curvature = -0.1,
segment.ncp = 3,
segment.angle = 20,
direction = "y", hjust = "left")+
geom_label_repel(
data=label[which(label$UMAP_1<0 &label$UMAP_2<3),],
aes(x= UMAP_1, y = UMAP_2,label=labels),
size=3,
nudge_x = -3,
box.padding = 0.5,
nudge_y = -12,
segment.curvature = -0.1,
segment.ncp = 3,
segment.angle = 20,
direction = "y", hjust = "left")
image.png
很显然,代码不能一次性,至少我没有方法能够一次性达到这种效果。所以遇到这种,直接上AI或者PS,省时省力!
网友评论