From 34802cda8ee91bfa2b39f65536387e7511de860d Mon Sep 17 00:00:00 2001 From: Michael Morley Date: Thu, 14 Oct 2021 15:56:01 -0400 Subject: [PATCH] Added sampleBarGraph function --- DESCRIPTION | 2 +- NAMESPACE | 1 + R/Plotting.R | 37 +++++++++++++++++++++++++++++++++++++ man/sampleBarGraph.Rd | 18 ++++++++++++++++++ 4 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 man/sampleBarGraph.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 651ab88..7d5181b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -8,4 +8,4 @@ Maintainer: Who to complain to Description: Functions to run Seurat, slingshot, monocle and other scRNA packages on single cell data License: What license is it under? LazyData: TRUE -RoxygenNote: 7.1.1 +RoxygenNote: 7.1.2 diff --git a/NAMESPACE b/NAMESPACE index 53b7fe3..8968fbf 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -27,6 +27,7 @@ export(plotLineangeGene) export(processExper) export(runPseudoTimeDGE) export(runSlingshot) +export(sampleBarGraph) export(setSDS) import(ComplexHeatmap) import(DoubletFinder) diff --git a/R/Plotting.R b/R/Plotting.R index 41e481c..315b53b 100644 --- a/R/Plotting.R +++ b/R/Plotting.R @@ -1,4 +1,41 @@ +#'Plot a stacked bar graph of per Sample contribtion +#' @param object Seurat object +#' @param group.by Variable for x axis typicall cluster or celltype +#' @param cols Color palette for grouping variable +#' @export + +sampleBarGraph <- function(object,group.by=NULL,col=NULL){ + if(is.null(group.by)){ + print("group.by cannot be null") + exit() + } + if(is.null(object$sample)){ + print("Cannot find Sample field in meta data") + exit() + } + + + p <- object@meta.data %>% + group_by(sample,!!sym(group.by)) %>% + summarise(n = n()) %>% + mutate(pct = n / sum(n)) %>% + ggplot(.,aes(x=!!sym(group.by),y=pct,fill=sample)) + + geom_bar(position="fill", stat="identity") + + scale_y_continuous(labels = scales::percent_format()) + + scale_fill_manual(values=col) + + theme_bw() + theme(legend.position = 'bottom') + + if(!is.null(col)){ + p <- p + scale_fill_manual(values=col) + } + p + + +} + + + #'Plot Pairwise PCA plots. #' @param object Seurat object #' @param dims Compute N PC dims diff --git a/man/sampleBarGraph.Rd b/man/sampleBarGraph.Rd new file mode 100644 index 0000000..5914f7d --- /dev/null +++ b/man/sampleBarGraph.Rd @@ -0,0 +1,18 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/Plotting.R +\name{sampleBarGraph} +\alias{sampleBarGraph} +\title{Plot a stacked bar graph of per Sample contribtion} +\usage{ +sampleBarGraph(object, group.by = NULL, col = NULL) +} +\arguments{ +\item{object}{Seurat object} + +\item{group.by}{Variable for x axis typicall cluster or celltype} + +\item{cols}{Color palette for grouping variable} +} +\description{ +Plot a stacked bar graph of per Sample contribtion +}