分享

关于单细胞番外Scillus包的一些问题修改示范

 TS的美梦 2022-05-13

我有话说:开始之前,啰嗦两句,很多小伙伴拿到代码一运行,出错了或者改动的时候立马就来问,有问题问其实很好,但是也要自己尝试,删除几个代码,自己调整几个参数都不是什么问题,在这个过程中,其实你对於这写内容就很熟悉了,也是一种成长进步,谁还不是在报错中开始的学习的,实在解决不了,可联系。


之前我们在单细胞系列提到过单细胞作图修饰的一个R包Scillus:

单细胞番外---单细胞数据分析及可视化集成R包Scillus(上)

单细胞番外---单细胞数据分析R包Scillus之marker基因可视化(中)

单细胞番外---单细胞R包Scillus之差异基因GSEA分析(下)

有小伙伴反应,我自己也发现了一个问题,例如在使用Scillus包中的plot_stat函数(方便计算细胞比例)的时候,如果想要看各个cluster的细胞比例是没有问题的。

library(Scillus)plot_stat(scedata,           plot_type = "prop_multi",           group_by = "group",           text_size = 8)

但是我们也会发现一个问题,当我们注释好细胞类型之后,这个函数无法展示各个细胞类型的比例,而且图的排版也是固定的,只有4列,颜色也是默认的。这是因为作者在写函数的时候就已经固定了是针对seurat_clusters,所以无法展示细胞类型的比例。为了解决这个问题,我们可以对作者的原函数进行修改。

library(Scillus)cellcolor <- c("#339966","#FF6633","#FFCC33",   "#99CC66","#666699","#336699",   "#FFCCCC","#E64B35FF","#00A087FF",   "#B09C85FF","#FFFF66")#修改后的plot函数plot_stat <- function(dataset,                       plot_type,                       group_by = "sample",                      pal_setup = 'Set2',                      plot_ratio = 1,                      text_size = 10,                      tilt_text = FALSE) {
if (is.data.frame(pal_setup)) { pal <- pal_setup[pal_setup[[1]] == group_by,][[2]] } else { pal <- pal_setup }
stat <- tibble::tibble(group = dataset[[group_by]][[1]], cluster = dataset[['celltype']][[1]]) stat %<>% group_by(.data$group, .data$cluster) %>% summarise(n = n()) %>% mutate(freq = n / sum(n))
ncolors <- if (plot_type == 'prop_fill') { length(unique(dataset[['celltype']][[1]])) } else { length(unique(dataset[[group_by]][[1]])) }
colors <- cellcolor
thm <- theme(aspect.ratio = plot_ratio, legend.title = element_text(size = text_size), legend.text = element_text(size = text_size), axis.title = element_text(size = text_size), axis.text = element_text(size = text_size), axis.title.x = element_blank() ) + theme_bw()
thm2 <- theme(legend.position = "none") thm3 <- theme(axis.text.x = element_text(angle = 45, vjust = 0.5))
switch(plot_type, group_count = stat %>% group_by(.data$group) %>% summarise(sum(n)) %>% ggplot(aes(x = .data$group, y = .data$`sum(n)`)) + geom_col(aes(fill = .data$group)) + geom_text(aes(label = .data$`sum(n)`), vjust = -0.5, size = text_size * 0.35) + scale_fill_manual(values = colors) + labs(x = group_by, y = "Number of Cells") + thm + thm2 + if (tilt_text) {thm3},
prop_fill = ggplot(stat) + geom_bar(aes(x = .data$group, y = .data$freq, fill = .data$cluster), position = "fill", stat = "identity") + scale_y_continuous(labels = scales::percent) + scale_fill_manual(values = colors, name = "Cluster") + labs(x = group_by, y = "Proportion") + thm + if (tilt_text) {thm3},
prop_multi = stat %>% mutate(freq = round(.data$freq, 3)) %>% ggplot() + geom_bar(aes(x = .data$group, y = .data$freq, fill = .data$group), stat = "identity") + geom_text(aes(x = .data$group, y = .data$freq, label = scales::percent(.data$freq)), vjust = -0.5, size = text_size * 0.35) + scale_y_continuous(expand = expansion(mult = c(0, 0.1)), labels = scales::percent_format()) + facet_wrap(~ cluster, ncol = 5, scales = "free") + scale_fill_manual(values = colors, name = "Group") + labs(x = NULL, y = "Proportion") + theme(strip.text.x = element_text(size = text_size)) + thm + thm2 + if (tilt_text) {thm3},
stop("Unknown plot type") )}
#先运行函数plot_stat(scedata, plot_type = "prop_multi", group_by = "group",  text_size = 8)

原函数中图形形式、颜色、排版都可以自行修饰!

Scillus包原函数在github,链接:https://github.com/xmc811/Scillus

这个包其他的函数,类似于热图等都可以自行修饰,形成自己的风格。这个包在作图方面真的还是很方便的,值得自己修饰下进行使用。

想要更多内容或者想和其他小伙伴交流的可加入我们的QQ群,入群有门槛哦,但是也有更多资源和优惠,详情请联系联系作者:

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多