From 29c73d7a6d9e6c3661fe2b1cd23d4443a69a9953 Mon Sep 17 00:00:00 2001 From: Jasper Spitzer <97746217+jaspitzer@users.noreply.github.com> Date: Tue, 2 Jun 2026 10:11:26 +0300 Subject: [PATCH] added a plotting function for reduced dim --- .gitignore | 3 +- NAMESPACE | 1 + R/plotting_functions.R | 61 +++++++++++++++++++++++++++++++++++++++++ man/plot_reduced_dim.Rd | 38 +++++++++++++++++++++++++ 4 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 R/plotting_functions.R create mode 100644 man/plot_reduced_dim.Rd diff --git a/.gitignore b/.gitignore index 070fc176..41c82c13 100755 --- a/.gitignore +++ b/.gitignore @@ -30,4 +30,5 @@ renv/settings.json .Rprofile tidybulk.Rcheck/* ..Rcheck/* -vignettes/cache/* \ No newline at end of file +vignettes/cache/* +tidybulk_check.Rproj diff --git a/NAMESPACE b/NAMESPACE index 0f444158..60582b36 100755 --- a/NAMESPACE +++ b/NAMESPACE @@ -22,6 +22,7 @@ export(log10_reverse_trans) export(logit_trans) export(pivot_sample) export(pivot_transcript) +export(plot_reduced_dim) export(quantile_normalise_abundance) export(reduce_dimensions) export(remove_redundancy) diff --git a/R/plotting_functions.R b/R/plotting_functions.R new file mode 100644 index 00000000..18a45dc7 --- /dev/null +++ b/R/plotting_functions.R @@ -0,0 +1,61 @@ +#' Plot reduced dimensions +#' +#' @param SE A 'SummarizedExperiment' +#' @param VAR A Symbol. What variable in colData to be used for color coding +#' @param method A character string. IndicatingMethod of dimensionality reduction to be ploted +#' @param dims Vector of dimensions to be plotted +#' +#' @returns A ggplot object +#' @export +#' +#' @examples +#' ## Load airway dataset for examples +#' data('airway', package = 'airway') +#' # Ensure a 'condition' column exists for examples expecting it +#' +#' SummarizedExperiment::colData(airway)$condition <- SummarizedExperiment::colData(airway)$dex +#' +#' +#' counts.PCA = +#' airway |> +#' identify_abundant() |> +#' reduce_dimensions(assay = "counts", method="PCA", .dims = 3) +#' +#' plot_reduced_dim(counts.PCA, condition, "PCA", 1:2) +plot_reduced_dim <- function(SE, VAR, method = "PCA", dims = 1:2){ + varname <- rlang::ensym(VAR) + + dim_type <- dplyr::case_when( + stringr::str_to_lower(method) == stringr::str_to_lower("PCA") ~ "pca", + stringr::str_to_lower(method) == stringr::str_to_lower("MDS") ~ "mds", + stringr::str_to_lower(method) == stringr::str_to_lower("tSNE") ~ "tsne", + stringr::str_to_lower(method) == stringr::str_to_lower("UMAP") ~ "umap") + + coord_naming <- dplyr::case_when( + dim_type == "pca" ~ "PC", + dim_type == "mds" ~ "Dim", + dim_type == "tsne" ~ "tSNE", + dim_type == "umap" ~ "UMAP") + + df <- SE %>% + pivot_sample() + + coord_names <- names(df)[stringr::str_detect(names(df), paste0("^", coord_naming, "\\d+"))] + coord_names <- coord_names[dims] + + + p <- SE |> + pivot_sample() |> + ggplot2::ggplot(ggplot2::aes(.data[[coord_names[1]]], .data[[coord_names[2]]], color = !!varname))+ + ggplot2::geom_point() + + if(dim_type == "pca"){ + pca_res <- S4Vectors::metadata(SE)$tidybulk$PCA + vars <- pca_res[[1]]^2 / sum(pca_res[[1]]^2) + + p <- p + + ggplot2::labs(x = paste0("PC1 (", round(vars[1] * 100), "% variance explained)"), + y = paste0("PC2 (", round(vars[2] * 100), "% variance explained)")) + } + return(p) +} \ No newline at end of file diff --git a/man/plot_reduced_dim.Rd b/man/plot_reduced_dim.Rd new file mode 100644 index 00000000..d19c8617 --- /dev/null +++ b/man/plot_reduced_dim.Rd @@ -0,0 +1,38 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/plotting_functions.R +\name{plot_reduced_dim} +\alias{plot_reduced_dim} +\title{Plot reduced dimensions} +\usage{ +plot_reduced_dim(SE, VAR, method = "PCA", dims = 1:2) +} +\arguments{ +\item{SE}{A 'SummarizedExperiment'} + +\item{VAR}{A Symbol. What variable in colData to be used for color coding} + +\item{method}{A character string. IndicatingMethod of dimensionality reduction to be ploted} + +\item{dims}{Vector of dimensions to be plotted} +} +\value{ +A ggplot object +} +\description{ +Plot reduced dimensions +} +\examples{ +## Load airway dataset for examples +data('airway', package = 'airway') + # Ensure a 'condition' column exists for examples expecting it + + SummarizedExperiment::colData(airway)$condition <- SummarizedExperiment::colData(airway)$dex + + +counts.PCA = + airway |> + identify_abundant() |> + reduce_dimensions(assay = "counts", method="PCA", .dims = 3) + + plot_reduced_dim(counts.PCA, condition, "PCA", 1:2) +}