美文网首页
更紧凑的堆叠小提琴图

更紧凑的堆叠小提琴图

作者: 单链结合蛋白 | 来源:发表于2022-03-04 10:00 被阅读0次

seurat自己的vlnplot出的图感觉有点丑丑的,网上找到了scRNAseq || 堆叠小提琴图(StackedVlnPlot)的4种实现方式。这个教程基本实现了我想要堆叠小提琴图的效果。同时也推一下他们的微信公众号上教程:R函数实现单细胞StackedVlnPlot.
不过,我在实际使用的过程种遇到了一点小问题。
他们的原始代码为:

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)
}

#函数调用
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

可以看到有2个小问题:

  • 坐标轴的问题没有旋转
  • patchwork的拼图没有很紧凑,导致多个gene进行堆叠的时候,显得很空,导致不适合很多基因一起作图

基于上面的小问题,修改了一下上面的函数。

#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)
}
#函数调用
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

注意,部分标签信息已经隐去。
可以看到修改的图更加紧凑,可以用于大量marker gene的绘制。

相关文章

网友评论

      本文标题:更紧凑的堆叠小提琴图

      本文链接:https://www.haomeiwen.com/subject/bncvrrtx.html