diff --git a/.Rbuildignore b/.Rbuildignore index daa004f..7e51386 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -1,6 +1,4 @@ LICENSE -.*rds$ -.*RDS$ ^\.lintr$ ^\.vscode ^.*\.Rproj$ diff --git a/DESCRIPTION b/DESCRIPTION index 7f221fb..278a5d1 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: rliger Version: 2.1.0 -Date: 2024-10-25 +Date: 2024-10-29 Type: Package Title: Linked Inference of Genomic Experimental Relationships Description: Uses an extension of nonnegative matrix factorization to identify shared and dataset-specific factors. See Welch J, Kozareva V, et al (2019) , and Liu J, Gao C, Sodicoff J, et al (2020) for more details. diff --git a/R/visualization.R b/R/visualization.R index 84dbcae..daefb87 100644 --- a/R/visualization.R +++ b/R/visualization.R @@ -339,9 +339,13 @@ plotGeneDetectedViolin <- function( #' function arguments before matching the \code{...} arguments. #' @param object A \linkS4class{liger} object. #' @param gene Character vector of gene names. -#' @param groupBy Names of available categorical variable in \code{cellMeta} -#' slot. Use \code{FALSE} for no grouping. Default \code{NULL} looks clustering -#' result but will not group if no clustering found. +#' @param groupBy The name of an available categorical variable in +#' \code{cellMeta} slot. This forms the main x-axis columns. Use \code{FALSE} +#' for no grouping. Default \code{NULL} looks clustering result but will not +#' group if no clustering is found. +#' @param colorBy The name of another categorical variable in \code{cellMeta} +#' slot. This split the main grouping columns and color the violins. Default +#' \code{NULL} will not split and color the violins. #' @param box Logical, whether to add boxplot. Default \code{FALSE}. #' @param boxAlpha Numeric, transparency of boxplot. Default \code{0.1}. #' @param yFunc Function to transform the y-axis. Default is @@ -358,10 +362,11 @@ plotClusterGeneViolin <- function( object, gene, groupBy = NULL, + colorBy = NULL, box = FALSE, boxAlpha = 0.1, yFunc = function(x) log1p(x*1e4), - showLegend = FALSE, + showLegend = !is.null(colorBy), xlabAngle = 40, ... ) { @@ -382,19 +387,38 @@ plotClusterGeneViolin <- function( groupBy <- "group" featureDF[[groupBy]] <- factor("All") } - p <- featureDF %>% + if (!is.null(colorBy)) { + colorVar <- .fetchCellMetaVar( + object = object, variables = colorBy, checkCategorical = TRUE + ) + featureDF[[colorBy]] <- factor(colorVar) + } + + featureDF <- featureDF %>% .pivot_longer( cols = seq(ngene), names_to = "gene", values_to = "Expression" ) %>% - dplyr::mutate(gene = factor(.data[["gene"]], levels = geneUse)) %>% - ggplot2::ggplot(ggplot2::aes( - x = .data[[groupBy]], - y = .data[["Expression"]], - fill = .data[[groupBy]] - )) + - ggplot2::geom_violin() + dplyr::mutate(gene = factor(.data[["gene"]], levels = geneUse)) + if (!is.null(colorBy)) { + p <- ggplot2::ggplot( + data = featureDF, + mapping = ggplot2::aes( + x = .data[[groupBy]], + y = .data[["Expression"]], + fill = .data[[colorBy]] + )) + } else { + p <- ggplot2::ggplot( + data = featureDF, + mapping = ggplot2::aes( + x = .data[[groupBy]], + y = .data[["Expression"]], + fill = .data[[groupBy]] + )) + } + p <- p + ggplot2::geom_violin() if (isTRUE(box)) { p <- p + ggplot2::geom_boxplot(alpha = boxAlpha) } diff --git a/cran-comments.md b/cran-comments.md index 0772fc2..3ddfd5a 100644 --- a/cran-comments.md +++ b/cran-comments.md @@ -15,8 +15,6 @@ Suggests or Enhances not in mainstream repositories: Availability using Additional_repositories specification: DoubletFinder yes https://blaserlab.r-universe.dev RcppPlanc yes https:/welch-lab.r-universe.dev - -Package has a VignetteBuilder field but no prebuilt vignette index. ``` ``` diff --git a/man/plotClusterGeneViolin.Rd b/man/plotClusterGeneViolin.Rd index ce005f4..dcb7542 100644 --- a/man/plotClusterGeneViolin.Rd +++ b/man/plotClusterGeneViolin.Rd @@ -8,10 +8,11 @@ plotClusterGeneViolin( object, gene, groupBy = NULL, + colorBy = NULL, box = FALSE, boxAlpha = 0.1, yFunc = function(x) log1p(x * 10000), - showLegend = FALSE, + showLegend = !is.null(colorBy), xlabAngle = 40, ... ) @@ -21,9 +22,14 @@ plotClusterGeneViolin( \item{gene}{Character vector of gene names.} -\item{groupBy}{Names of available categorical variable in \code{cellMeta} -slot. Use \code{FALSE} for no grouping. Default \code{NULL} looks clustering -result but will not group if no clustering found.} +\item{groupBy}{The name of an available categorical variable in +\code{cellMeta} slot. This forms the main x-axis columns. Use \code{FALSE} +for no grouping. Default \code{NULL} looks clustering result but will not +group if no clustering is found.} + +\item{colorBy}{The name of another categorical variable in \code{cellMeta} +slot. This split the main grouping columns and color the violins. Default +\code{NULL} will not split and color the violins.} \item{box}{Logical, whether to add boxplot. Default \code{FALSE}.} diff --git a/tests/testthat/test_visualization.R b/tests/testthat/test_visualization.R index 34d52e1..66fffd8 100644 --- a/tests/testthat/test_visualization.R +++ b/tests/testthat/test_visualization.R @@ -117,7 +117,7 @@ test_that("Violin plots", { ) expect_gg( - plotClusterGeneViolin(pbmcPlot, "S100A8", box = TRUE), + plotClusterGeneViolin(pbmcPlot, "S100A8", box = TRUE, colorBy = "dataset"), plotClusterGeneViolin(pbmcPlot, "S100A8", groupBy = FALSE) ) # General