seurat自己的vlnplot出的圖感覺有點(diǎn)丑丑的,網(wǎng)上找到了scRNAseq || 堆疊小提琴圖(StackedVlnPlot)的4種實(shí)現(xiàn)方式。這個(gè)教程基本實(shí)現(xiàn)了我想要堆疊小提琴圖的效果。同時(shí)也推一下他們的微信公眾號(hào)上教程:R函數(shù)實(shí)現(xiàn)單細(xì)胞StackedVlnPlot.
不過,我在實(shí)際使用的過程種遇到了一點(diǎn)小問題。
他們的原始代碼為:
library(Seurat)
library(ggplot2)
modify_vlnplot<- function(obj,
feature,
pt.size = 0,
plot.margin = unit(c(-0.75, 0, -0.75, 0), "cm"),
...) {
p<- VlnPlot(obj, features = feature, pt.size = pt.size, ... ) +
xlab("") + ylab(feature) + ggtitle("") +
theme(legend.position = "none",
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks.x = element_blank(),
axis.ticks.y = element_line(),
axis.title.y = element_text(size = rel(1), angle = 0, vjust = 0.5),
plot.margin = plot.margin )
return(p)
}
## main function
StackedVlnPlot<- function(obj, features,
pt.size = 0,
plot.margin = unit(c(-0.75, 0, -0.75, 0), "cm"),
...) {
plot_list<- purrr::map(features, function(x) modify_vlnplot(obj = obj,feature = x, ...))
plot_list[[length(plot_list)]]<- plot_list[[length(plot_list)]] +
theme(axis.text.x=element_text(), axis.ticks.x = element_line())
p<- patchwork::wrap_plots(plotlist = plot_list, ncol = 1)
return(p)
}
#函數(shù)調(diào)用
mycols <- c(brewer.pal(n = 9, name = 'Set1'),
brewer.pal(n = 8, name = 'Set2'),
brewer.pal(n =11, name = 'Set3'),
brewer.pal(n =8, name = 'Accent'),
brewer.pal(n =8, name = 'Dark2'),
brewer.pal(n =12, name = 'Paired'),
brewer.pal(n =9, name = 'Pastel1'),
brewer.pal(n =8, name = 'Pastel2'))
#mycols2 <- names(sort(table(mycols ))[sort(table(mycols )) == 1])
mycols <- mycols[mycols %in% names(sort(table(mycols ))[sort(table(mycols )) == 1])]
StackedVlnPlot(lmg02P, c('CD3D', 'GZMB','CD68','CD163', 'KRT18','KRT19','EPCAM'), pt.size=0, cols=mycols)
得到的結(jié)果示例:

image.png
可以看到有2個(gè)小問題:
- 坐標(biāo)軸的問題沒有旋轉(zhuǎn)
- patchwork的拼圖沒有很緊湊,導(dǎo)致多個(gè)gene進(jìn)行堆疊的時(shí)候,顯得很空,導(dǎo)致不適合很多基因一起作圖
基于上面的小問題,修改了一下上面的函數(shù)。
#self modify for more compact.
modify_vlnplot <- function(obj, feature, pt.size = 0, plot.margin = unit(c(-0.75, 0, -0.75, 0), "cm"),...) {
p <- VlnPlot(obj, features = feature, pt.size = pt.size, ... ) +
theme_void() +
ylab(feature) +
theme(legend.position = 'none',
plot.margin = plot.margin,
title = element_blank(),
axis.title.y = element_text(hjust = 0.5, angle = 0)
)
return(p)
}
## main function
StackedVlnPlot <- function(obj, features, pt.size = 0, plot.margin = unit(c(-0.75, 0, -0.75, 0), "cm"), ...) {
plot_list <- purrr::map(features, function(x) modify_vlnplot(obj = obj,feature = x, ...))
plot_list[[length(plot_list)]]<- plot_list[[length(plot_list)]] +
theme(axis.text.x=element_text(angle = 90), axis.ticks.x = element_line())
p <- patchwork::wrap_plots(plotlist = plot_list, ncol = 1)
return(p)
}
#函數(shù)調(diào)用
mycols <- c(brewer.pal(n = 9, name = 'Set1'),
brewer.pal(n = 8, name = 'Set2'),
brewer.pal(n =11, name = 'Set3'),
brewer.pal(n =8, name = 'Accent'),
brewer.pal(n =8, name = 'Dark2'),
brewer.pal(n =12, name = 'Paired'),
brewer.pal(n =9, name = 'Pastel1'),
brewer.pal(n =8, name = 'Pastel2'))
#mycols2 <- names(sort(table(mycols ))[sort(table(mycols )) == 1])
mycols <- mycols[mycols %in% names(sort(table(mycols ))[sort(table(mycols )) == 1])]
StackedVlnPlot(lmg02P, c('CD3D', 'GZMB','CD68','CD163', 'KRT18','KRT19','EPCAM'), pt.size=0, cols=mycols)
修改后的畫圖后的示例:

image.png
注意,部分標(biāo)簽信息已經(jīng)隱去。
可以看到修改的圖更加緊湊,可以用于大量marker gene的繪制。