有时候看单细胞的降维聚类图,图例那里会显示亚群(或者cluster)的标签,有时候可能还有好几个,所以就在想怎么通过ggplot2去添加,当然可以通过手动的一行行命令(比如annotate或者geom_text)或者AI去修图实现。查了一些资料(https://mp.weixin.qq.com/s/1KxCiADmlG8UaCcraXaSWw),做了一些测试(http://eyangzhen.com/16073.html)。
用这个群主的测试数据来学习。
library(ggplot2)
library(dplyr)
library(tidyverse)
nature_col = c("#AEC7E8","#FFBB78","#9467BD","#7200DA",
"#17BECF","#FF7F0E","#C49C94","#2CA02C","#8C564B",
"#E377C2","#D62728","#FF9896","#98DF8A","#BCBD22","#C5B0D5")
df <- read.csv("umap.csv")
colnames(df) <- c("ID","UMAP1","UMAP2","Cluster")
df$Cluster <- factor(df$Cluster,levels=cluster_order ) #指定显示顺序
先画一个大众版本的UMAP图
ggplot(df,aes(x= UMAP1 , y = UMAP2 ,col=Cluster)) +
geom_point(size = 2, shape=16)+
scale_color_manual("",values = nature_col)+
theme_bw()+
theme(panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
legend.title = element_blank(),
legend.key=element_rect(fill='white'),
legend.text = element_text(size=15),
legend.key.size=unit(0.8,'cm'),
axis.title = element_text(colour = 'black', size = 15, hjust = 0),
axis.text = element_blank(),
axis.ticks = element_blank())+
guides(color = guide_legend(override.aes = list(size=6)))
对于小箭头的修饰,我们测试用geom_segment函数来实现。
ggplot(df,aes(x= UMAP1 , y = UMAP2 ,col=Cluster)) +
geom_point(size = 2, shape=16)+
scale_color_manual("",values = nature_col)+
theme(panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.background = element_blank(),
legend.title = element_blank(),
legend.key=element_rect(fill='white'),
legend.text = element_text(size=15),
legend.key.size=unit(0.6,'cm'),
axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank())+
guides(color = guide_legend(override.aes = list(size=6)))+
geom_segment(aes(x = min(UMAP1) , y = min(UMAP2),xend = min(UMAP1)+3, yend = min(UMAP2)),
colour = "black", size=0.5,
arrow = arrow(length = unit(0.2,"cm"),
type = "closed"))+
geom_segment(aes(x = min(UMAP1), y = min(UMAP2),xend = min(UMAP1),yend = min(UMAP2)+1.5),
colour = "black", size=0.5,arrow = arrow(length = unit(0.2,"cm"),
type = "closed"))+
annotate("text", x = min(df$UMAP1) +1.4, y = min(df$UMAP2) -0.3, label = "UMAP1",
color="black",size = 5) +
annotate("text", x = min(df$UMAP1) -0.8, y = min(df$UMAP2) + 0.8, label = "UMAP2",
color="black",size = 5,angle=90)
注:但是我觉得不太好控制的是这个箭头的长度,和X轴和Y轴的scale有关,我也是不断的尝试了几个不同的数字,看看效果决定的。
下面添加UMAP上的cluster数字。
cell <- summarise(group_by(df, Cluster),
UMAP1 = median(UMAP1),
UMAP2 = median(UMAP2))
rownames(cell) <- cell$Cluster
a <- c(1:15)
cell$ID <- a
ggplot(df,aes(x= UMAP1 , y = UMAP2 ,col=Cluster)) +
geom_point(size = 2, shape=16)+
scale_color_manual("",values = nature_col)+
theme_bw()+
theme(panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
legend.title = element_blank(),
legend.key=element_rect(fill='white'),
legend.text = element_text(size=15),
legend.key.size=unit(0.8,'cm'),
axis.title = element_text(colour = 'black', size = 15, hjust = 0),
axis.text = element_blank(),
axis.ticks = element_blank())+
guides(color = guide_legend(override.aes = list(size=6)))+
geom_text_repel(data=cell, aes(label=ID),color="black", size=6, point.padding = 0.3)
其实右边的legend有三部分组成,带颜色的点,点上的数字,以及右边的代表cluster的text。我们可以单独控制右边的legend,然后把两个图合起来就成了。
右边的legend,对于带颜色的点的控制,X坐标其实都是一样的,Y坐标的依次递增就可以。所以我们可以根据前面生成的cell的变量,在其中控制X和Y的位置就行。
我们可以在cell里面添加X和Y坐标,X坐标设置为相同的1,Y坐标从Mast cell开始为一就成。
B <- cell
B$x <- 1
B$lab <- c(15:1)
B$y <- c(15:1)
#我们就生成了一个控制位置和显示数字的变量。
ggplot(B,aes(x = x, y = y)) +
geom_point(shape = 21, size = 10, aes(x = x, y = y, fill=Cluster)) +
geom_text(aes(label = lab, x = x, y = y), size = 6)+
theme(panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.background = element_blank(),
legend.position = 'none',
axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank())+
scale_fill_manual(values = rev(nature_col))+
annotate("text", x = 1, y = 16, label = "Cluster",
color="black",size = 6)
#这样就可以生成legend的一半了,下面显示表明cluster的text就成。
ggplot(B,aes(x = x, y = y)) +
geom_point(shape = 21, size = 10, aes(x = x, y = y, fill=Cluster)) +
geom_text(aes(label = lab, x = x, y = y), size = 6)+
theme(panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.background = element_blank(),
legend.position = 'none',
axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank())+
scale_fill_manual(values = rev(nature_col))+
annotate("text", x = 1, y = 16, label = "Cluster",
color="black",size = 6)+
geom_text(aes(label = Cluster, x = x+0.001,
y = y), size = 5, hjust=0)+
scale_x_continuous(expand=c(-0.01,0.01))
这样就获得了右边的legend了。
p <- ggplot(df,aes(x= UMAP1 , y = UMAP2 ,col=Cluster)) +
geom_point(size = 2, shape=16)+
scale_color_manual("",values = nature_col)+
theme_bw()+
theme(panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
legend.position = 'none',
axis.title = element_text(colour = 'black', size = 15, hjust = 0),
axis.text = element_blank(),
axis.ticks = element_blank())+
geom_text_repel(data=cell, aes(label=ID),color="black", size=6, point.padding = 0.3)
leg <- ggplot(B,aes(x = x, y = y)) +
geom_point(shape = 21, size = 10, aes(x = x, y = y, fill=Cluster)) +
geom_text(aes(label = lab, x = x, y = y), size = 6)+
theme(panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.background = element_blank(),
legend.position = 'none',
axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank())+
scale_fill_manual(values = rev(nature_col))+
annotate("text", x = 1, y = 16, label = "Cluster",
color="black",size = 6)+
geom_text(aes(label = Cluster, x = x+0.01,
y = y), size = 5, hjust=0)+
scale_x_continuous(expand=c(-0.01,0.01))
#合并两个图片
library(cowplot)
plotlist <- list(p, leg)
plot_grid(plotlist = plotlist, ncol = 2, align="hv", rel_widths = c(5,2))
有时候,我们还会碰到多个cluster对应同一个细胞类型的情况,这个时候,我们就需要在坐标显示的位置稍微控制一下,Y坐标相同,X坐标稍微不一样一下。
还是用我们上面的B变量为例子。
C <- B[,c(1,5,6,7)]
C[16,1] <- "YSMP"
C[16,2] <- 1-0.001
C[16,3] <- 16
C[16,4] <- 15
C[17,1] <- "YSMP"
C[17,2] <- 1-0.002
C[17,3] <- 17
C[17,4] <- 15
C[18,1] <- "Mast cell"
C[18,2] <- 1-0.001
C[18,3] <- 18
C[18,4] <- 1
我们又认为的望里面添加了3个量。
#还是利用我们前面对于legend的控制来实现。
ggplot(C,aes(x = x, y = y)) +
geom_point(shape = 21, size = 10, aes(x = x, y = y, fill=Cluster)) +
geom_text(aes(label = lab, x = x, y = y), size = 6)+
theme(panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.background = element_blank(),
legend.position = 'none',
axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank())+
scale_fill_manual(values = rev(nature_col))+
annotate("text", x = 1, y = 16, label = "Cluster",
color="black",size = 6)+
geom_text(data=B,aes(label = Cluster, x = x+0.001,
y = y), size = 5, hjust=0)+
scale_x_continuous(expand=c(-0.01,0.01))
#只不过新生成的C来控制legend的左半部分,右半部分还是有B来控制实现。