我有話說:開始之前,啰嗦兩句,很多小伙伴拿到代碼一運行,出錯了或者改動的時候立馬就來問,有問題問其實很好,但是也要自己嘗試,刪除幾個代碼,自己調(diào)整幾個參數(shù)都不是什么問題,在這個過程中,其實你對於這寫內(nèi)容就很熟悉了,也是一種成長進步,誰還不是在報錯中開始的學(xué)習(xí)的,實在解決不了,可聯(lián)系。 之前我們在單細胞系列提到過單細胞作圖修飾的一個R包Scillus:
單細胞番外---單細胞數(shù)據(jù)分析及可視化集成R包Scillus(上)
單細胞番外---單細胞數(shù)據(jù)分析R包Scillus之marker基因可視化(中)
單細胞番外---單細胞R包Scillus之差異基因GSEA分析(下)
有小伙伴反應(yīng),我自己也發(fā)現(xiàn)了一個問題,例如在使用Scillus包中的plot_stat函數(shù)(方便計算細胞比例)的時候,如果想要看各個cluster的細胞比例是沒有問題的。 library(Scillus) plot_stat(scedata, plot_type = "prop_multi", group_by = "group", text_size = 8)

但是我們也會發(fā)現(xiàn)一個問題,當(dāng)我們注釋好細胞類型之后,這個函數(shù)無法展示各個細胞類型的比例,而且圖的排版也是固定的,只有4列,顏色也是默認(rèn)的。這是因為作者在寫函數(shù)的時候就已經(jīng)固定了是針對seurat_clusters,所以無法展示細胞類型的比例。為了解決這個問題,我們可以對作者的原函數(shù)進行修改。 library(Scillus) cellcolor <- c("#339966","#FF6633","#FFCC33", "#99CC66","#666699","#336699", "#FFCCCC","#E64B35FF","#00A087FF", "#B09C85FF","#FFFF66") #修改后的plot函數(shù) 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") ) }
#先運行函數(shù) plot_stat(scedata, plot_type = "prop_multi", group_by = "group", text_size = 8)

原函數(shù)中圖形形式、顏色、排版都可以自行修飾!
Scillus包原函數(shù)在github,鏈接:https://github.com/xmc811/Scillus。 這個包其他的函數(shù),類似于熱圖等都可以自行修飾,形成自己的風(fēng)格。這個包在作圖方面真的還是很方便的,值得自己修飾下進行使用。
想要更多內(nèi)容或者想和其他小伙伴交流的可加入我們的QQ群,入群有門檻哦,但是也有更多資源和優(yōu)惠,詳情請聯(lián)系聯(lián)系作者:
|