1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| f_VlnBoxPlot_gene <- function(scRNAo, groupN, sampleN, geneN, rmZero=F){ res <- data.frame(geneID=colnames(scRNAo)) res[['groupN']] = scRNAo[[groupN]][res$geneID,] res[['sampleN']] = scRNAo[[sampleN]][res$geneID,] res[['value']] = scRNAo@assays$RNA@data[geneN, res$geneID] if(rmZero){ subset(res, value > 0) }else{ res } } require(ggplot2) f_VlnBoxPlot_df <- function(df, geneN, groupN='groupN'){ p <- ggplot(df, aes(x=!!sym(groupN), y=value, fill= !!sym(groupN))) p <- p + theme_bw() + NoLegend() p <- p + geom_violin() p <- p + stat_ydensity(trim = TRUE, scale = 'width', adjust = 1) p <- p + geom_boxplot(width=0.618) p <- p + stat_summary(fun="mean",geom="point",color='white') p <- p + labs(x=NULL, y=NULL) p <- p + scale_y_continuous('', breaks = floor(max(layer_scales(p)$y$range$range)))
p <- p + theme(plot.margin = unit(c(0,0,0,0), "cm")) p <- p + facet_grid(sampleN ~ .) p <- p + labs(title = geneN) + theme(plot.title = element_text(hjust = 0.5)) p <- p + theme(axis.text.x=element_text(hjust = 1, angle = 45)) p }
|