From 93eed97d165aa477498501af9a842e4ed44a0388 Mon Sep 17 00:00:00 2001 From: "Stephanie J. Spielman" Date: Fri, 5 Jan 2024 13:30:37 -0500 Subject: [PATCH 01/17] Update legend position and add label wrapping to the lump function. The default level of wrapping plays well with the legend position --- templates/qc_report/celltypes_qc.rmd | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/templates/qc_report/celltypes_qc.rmd b/templates/qc_report/celltypes_qc.rmd index fe6ee854..69f70672 100644 --- a/templates/qc_report/celltypes_qc.rmd +++ b/templates/qc_report/celltypes_qc.rmd @@ -42,6 +42,7 @@ format_celltype_n_table <- function(df) { #' Function to lump celltype columns in an existing data frame for all of the #' following columns, if they exist: `_celltype_annotation`. +#' The cell types will also be renamed via wrapping at the given `wrap` level. #' The resulting lumped column will be named: #' `_celltype_annotation_lumped`. #' @@ -50,8 +51,16 @@ format_celltype_n_table <- function(df) { #' @param n_celltypes Number of groups to lump into, with rest put into "Other" group. Default is 7. #' #' @return Updated df with new column of lumped celltypes for each present method -lump_celltypes <- function(df, n_celltypes = 7) { +lump_wrap_celltypes <- function(df, n_celltypes = 7, wrap = 35) { df <- df |> + # First, wrap labels + dplyr::mutate( + across( + ends_with("_celltype_annotation"), + \(x) stringr::str_wrap(x, wrap) + ) + ) |> + # Now, apply factor lumping dplyr::mutate( across( ends_with("_celltype_annotation"), @@ -152,9 +161,12 @@ faceted_umap <- function(umap_df, ) ) + theme( - legend.position = c(.9, 0), - legend.justification = c("right", "bottom"), - legend.title.align = 0.5 + legend.position = c(0.95, 0), + legend.justification = c(1, -0.1), + legend.title.align = 0.5, + # use slightly smaller legend text, which helps legend fit and prevents + # long wrapped labels from bunching up + legend.text = element_text(size = rel(0.85)) ) return(faceted_umap) @@ -278,8 +290,8 @@ Clusters were calculated using the graph-based {metadata(processed_sce)$cluster_ ```{r, eval = has_umap} -# Create dataset for plotting UMAPs with lumped cell types -umap_df <- lump_celltypes(celltype_df) +# Create dataset for plotting UMAPs with lumped and label-wrapped cell types +umap_df <- lump_wrap_celltypes(celltype_df) ``` From d319c6036b98dfd849c2b7e26dd72a7206cd471a Mon Sep 17 00:00:00 2001 From: "Stephanie J. Spielman" Date: Fri, 5 Jan 2024 13:34:29 -0500 Subject: [PATCH 02/17] Fix factor bug - after lumping, they should still be in order of frequency --- templates/qc_report/celltypes_qc.rmd | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/templates/qc_report/celltypes_qc.rmd b/templates/qc_report/celltypes_qc.rmd index 69f70672..2d38f014 100644 --- a/templates/qc_report/celltypes_qc.rmd +++ b/templates/qc_report/celltypes_qc.rmd @@ -60,15 +60,19 @@ lump_wrap_celltypes <- function(df, n_celltypes = 7, wrap = 35) { \(x) stringr::str_wrap(x, wrap) ) ) |> - # Now, apply factor lumping + # Next, apply factor lumping, but ensure final order is via frequency with "All remaining cell types" at the end dplyr::mutate( across( ends_with("_celltype_annotation"), - \(x) forcats::fct_lump_n(x, n_celltypes, other_level = "All remaining cell types", ties.method = "first"), + \(x) { + x |> + forcats::fct_lump_n(n_celltypes, other_level = "All remaining cell types", ties.method = "first") |> + forcats::fct_infreq() |> + forcats::fct_relevel("All remaining cell types", after = Inf) + }, .names = "{.col}_lumped" ) ) - return(df) } From 22a1bebc8366d138d0876e80afd7672f08c4ca0c Mon Sep 17 00:00:00 2001 From: "Stephanie J. Spielman" Date: Fri, 5 Jan 2024 13:40:02 -0500 Subject: [PATCH 03/17] Add conditional legend placement using modulo --- templates/qc_report/celltypes_qc.rmd | 34 +++++++++++++++++++++------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/templates/qc_report/celltypes_qc.rmd b/templates/qc_report/celltypes_qc.rmd index 2d38f014..4e6f7de0 100644 --- a/templates/qc_report/celltypes_qc.rmd +++ b/templates/qc_report/celltypes_qc.rmd @@ -133,6 +133,12 @@ plot_umap <- function( #' In each panel, the cell type of interest is colored and all other cells are grey. faceted_umap <- function(umap_df, annotation_column) { + # Find total number of cell types for determining legend placement + total_celltypes <- umap_df |> + dplyr::pull({{ annotation_column }}) |> + levels() |> + length() + # color by the annotation column but only color one cell type at a time faceted_umap <- ggplot( umap_df, @@ -163,16 +169,28 @@ faceted_umap <- function(umap_df, size = 1.5 ) ) - ) + - theme( - legend.position = c(0.95, 0), - legend.justification = c(1, -0.1), - legend.title.align = 0.5, - # use slightly smaller legend text, which helps legend fit and prevents - # long wrapped labels from bunching up - legend.text = element_text(size = rel(0.85)) ) + # Determine legend placement based on total_celltypes + if (total_celltypes %% 3 != 0) { + faceted_umap <- faceted_umap + + theme( + legend.position = c(0.95, 0), + legend.justification = c(1, -0.1), + legend.title.align = 0.5, + # use slightly smaller legend text, which helps legend fit and prevents + # long wrapped labels from bunching up + legend.text = element_text(size = rel(0.85)) + ) + } else { + faceted_umap <- faceted_umap + + theme( + legend.position = "bottom" + ) + } + + + return(faceted_umap) } ``` From 3fa66d04f0e8038ea1997870a1936d07e33f3119 Mon Sep 17 00:00:00 2001 From: "Stephanie J. Spielman" Date: Fri, 5 Jan 2024 13:43:28 -0500 Subject: [PATCH 04/17] add dplyr:: for when this function is called from the supp report, which does not load dplyr --- templates/qc_report/celltypes_qc.rmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/qc_report/celltypes_qc.rmd b/templates/qc_report/celltypes_qc.rmd index 4e6f7de0..cc75a4e7 100644 --- a/templates/qc_report/celltypes_qc.rmd +++ b/templates/qc_report/celltypes_qc.rmd @@ -146,7 +146,7 @@ faceted_umap <- function(umap_df, ) + # set points for all "other" points geom_point( - data = select( + data = dplyr::select( umap_df, -{{ annotation_column }} ), color = "gray80", From 2addaad087bb0d5b1a9e17f02bccc41371f139a2 Mon Sep 17 00:00:00 2001 From: "Stephanie J. Spielman" Date: Fri, 5 Jan 2024 13:48:52 -0500 Subject: [PATCH 05/17] make sure we're using three columns --- templates/qc_report/celltypes_qc.rmd | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/templates/qc_report/celltypes_qc.rmd b/templates/qc_report/celltypes_qc.rmd index cc75a4e7..d12b98ed 100644 --- a/templates/qc_report/celltypes_qc.rmd +++ b/templates/qc_report/celltypes_qc.rmd @@ -155,7 +155,10 @@ faceted_umap <- function(umap_df, ) + # set points for desired cell type geom_point(size = 0.3, alpha = 0.5) + - facet_wrap(vars({{ annotation_column }})) + + facet_wrap( + vars({{ annotation_column }}), + ncol = 3 + ) + scale_color_brewer(palette = "Dark2") + # remove axis numbers and background grid scale_x_continuous(labels = NULL, breaks = NULL) + From 5b1732c50a420bd471220909c17b20ee51272d6c Mon Sep 17 00:00:00 2001 From: "Stephanie J. Spielman" Date: Fri, 5 Jan 2024 13:53:44 -0500 Subject: [PATCH 06/17] also account for unknown cell type in the refactoring --- templates/qc_report/celltypes_qc.rmd | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/templates/qc_report/celltypes_qc.rmd b/templates/qc_report/celltypes_qc.rmd index d12b98ed..043d2c38 100644 --- a/templates/qc_report/celltypes_qc.rmd +++ b/templates/qc_report/celltypes_qc.rmd @@ -60,7 +60,7 @@ lump_wrap_celltypes <- function(df, n_celltypes = 7, wrap = 35) { \(x) stringr::str_wrap(x, wrap) ) ) |> - # Next, apply factor lumping, but ensure final order is via frequency with "All remaining cell types" at the end + # Next, apply factor lumping, but ensure final order is via frequency with the "others" at the end dplyr::mutate( across( ends_with("_celltype_annotation"), @@ -68,7 +68,7 @@ lump_wrap_celltypes <- function(df, n_celltypes = 7, wrap = 35) { x |> forcats::fct_lump_n(n_celltypes, other_level = "All remaining cell types", ties.method = "first") |> forcats::fct_infreq() |> - forcats::fct_relevel("All remaining cell types", after = Inf) + forcats::fct_relevel("Unknown cell type", "All remaining cell types", after = Inf) }, .names = "{.col}_lumped" ) From dc5af08577cc7f6e6c4751129a367a02fbd50ca6 Mon Sep 17 00:00:00 2001 From: "Stephanie J. Spielman" Date: Fri, 5 Jan 2024 13:58:10 -0500 Subject: [PATCH 07/17] also if 1 cell type, legend goes on the bottom. This is almost certainly never going to happen though --- templates/qc_report/celltypes_qc.rmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/qc_report/celltypes_qc.rmd b/templates/qc_report/celltypes_qc.rmd index 043d2c38..bef9adb8 100644 --- a/templates/qc_report/celltypes_qc.rmd +++ b/templates/qc_report/celltypes_qc.rmd @@ -175,7 +175,7 @@ faceted_umap <- function(umap_df, ) # Determine legend placement based on total_celltypes - if (total_celltypes %% 3 != 0) { + if (total_celltypes %% 3 != 0 & total_celltypes != 1) { faceted_umap <- faceted_umap + theme( legend.position = c(0.95, 0), From ad591905749116bf69c73457a8a34fe04932f51a Mon Sep 17 00:00:00 2001 From: "Stephanie J. Spielman" Date: Fri, 5 Jan 2024 15:20:11 -0500 Subject: [PATCH 08/17] restore aspect.ratio = 1 and update legend placement to fit --- templates/qc_report/celltypes_qc.rmd | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/templates/qc_report/celltypes_qc.rmd b/templates/qc_report/celltypes_qc.rmd index bef9adb8..5db81f35 100644 --- a/templates/qc_report/celltypes_qc.rmd +++ b/templates/qc_report/celltypes_qc.rmd @@ -178,17 +178,19 @@ faceted_umap <- function(umap_df, if (total_celltypes %% 3 != 0 & total_celltypes != 1) { faceted_umap <- faceted_umap + theme( - legend.position = c(0.95, 0), - legend.justification = c(1, -0.1), + legend.position = c(0.68, 0.33), + legend.justification = c("left", "top"), legend.title.align = 0.5, # use slightly smaller legend text, which helps legend fit and prevents # long wrapped labels from bunching up - legend.text = element_text(size = rel(0.85)) + legend.text = element_text(size = rel(0.85)), + aspect.ratio = 1 ) } else { faceted_umap <- faceted_umap + theme( - legend.position = "bottom" + legend.position = "bottom", + aspect.ratio = 1 ) } From b00c9513a81ee069e8029b9d13265563144c8d4b Mon Sep 17 00:00:00 2001 From: "Stephanie J. Spielman" Date: Fri, 5 Jan 2024 16:08:16 -0500 Subject: [PATCH 09/17] Set umap height dynamically, but fix width at 8. --- templates/qc_report/celltypes_qc.rmd | 47 ++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/templates/qc_report/celltypes_qc.rmd b/templates/qc_report/celltypes_qc.rmd index 5db81f35..dcda2a2d 100644 --- a/templates/qc_report/celltypes_qc.rmd +++ b/templates/qc_report/celltypes_qc.rmd @@ -127,18 +127,14 @@ plot_umap <- function( #' Create a faceted UMAP panel where each panel has only one cell type colored #' #' @param umap_df Data frame with UMAP1 and UMAP2 columns +#' @param n_celltypes The number of cell types (facets) displayed in the plot #' @param annotation_column Column containing cell type annotations #' #' @return ggplot object containing a faceted UMAP where each cell type is a facet. #' In each panel, the cell type of interest is colored and all other cells are grey. faceted_umap <- function(umap_df, + n_celltypes, annotation_column) { - # Find total number of cell types for determining legend placement - total_celltypes <- umap_df |> - dplyr::pull({{ annotation_column }}) |> - levels() |> - length() - # color by the annotation column but only color one cell type at a time faceted_umap <- ggplot( umap_df, @@ -175,7 +171,7 @@ faceted_umap <- function(umap_df, ) # Determine legend placement based on total_celltypes - if (total_celltypes %% 3 != 0 & total_celltypes != 1) { + if (n_celltypes %% 3 != 0 & n_celltypes != 1) { faceted_umap <- faceted_umap + theme( legend.position = c(0.68, 0.33), @@ -368,31 +364,62 @@ All other cell types are grouped together and labeled "All remaining cell types" -```{r, eval = has_submitter && has_umap, message=FALSE, warning=FALSE, fig.height = 9, fig.width=9} +```{r} +# Function to determine plot height, and width should sort itself out? +determine_umap_height <- function(n_types) { + dplyr::case_when( + n_types <= 2 ~ 3, + n_types <= 3 ~ 4, + n_types <= 5 ~ 6, + n_types <= 6 ~ 7, + .default = 8 + ) +} +``` + +```{r, eval = has_submitter && has_umap} +submitter_n_celltypes <- length(levels(umap_df$submitter_celltype_annotation_lumped)) +submitter_height <- determine_umap_height(submitter_n_celltypes) +``` + +```{r, eval = has_submitter && has_umap, message=FALSE, warning=FALSE, fig.height = submitter_height, fig.width = 8} # umap for cell assign annotations faceted_umap( umap_df, + submitter_n_celltypes, submitter_celltype_annotation_lumped ) + ggtitle("UMAP colored by submitter-provided annotations") ``` -```{r, eval=has_singler && has_umap, message=FALSE, warning=FALSE, fig.height=9, fig.width=9} +```{r, eval = has_singler && has_umap} +singler_n_celltypes <- length(levels(umap_df$singler_celltype_annotation_lumped)) +singler_height <- determine_umap_height(singler_n_celltypes) +``` + + +```{r, eval=has_singler && has_umap, message=FALSE, warning=FALSE, fig.height = singler_height, fig.width = 8} # umap for cell assign annotations faceted_umap( umap_df, + singler_n_celltypes, singler_celltype_annotation_lumped ) + ggtitle("UMAP colored by SingleR annotations") ``` +```{r, eval = has_cellassign && has_umap} +cellassign_n_celltypes <- length(levels(umap_df$cellassign_celltype_annotation_lumped)) +cellassign_height <- determine_umap_height(cellassign_n_celltypes) +``` -```{r, eval = has_cellassign && has_umap, message=FALSE, warning=FALSE, fig.height = 9, fig.width=9} +```{r, eval = has_cellassign && has_umap, message=FALSE, warning=FALSE, fig.height = cellassign_height, fig.width = 8} # umap for cell assign annotations faceted_umap( umap_df, + cellassign_n_celltypes, cellassign_celltype_annotation_lumped ) + ggtitle("UMAP colored by CellAssign annotations") From 14d1d4bd47db7352262329224c8dba1e8a12be06 Mon Sep 17 00:00:00 2001 From: "Stephanie J. Spielman" Date: Fri, 5 Jan 2024 16:17:46 -0500 Subject: [PATCH 10/17] Plot height and legend tweaks after viewing all different options --- templates/qc_report/celltypes_qc.rmd | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/templates/qc_report/celltypes_qc.rmd b/templates/qc_report/celltypes_qc.rmd index dcda2a2d..c3847052 100644 --- a/templates/qc_report/celltypes_qc.rmd +++ b/templates/qc_report/celltypes_qc.rmd @@ -171,7 +171,13 @@ faceted_umap <- function(umap_df, ) # Determine legend placement based on total_celltypes - if (n_celltypes %% 3 != 0 & n_celltypes != 1) { + if (n_celltypes == 1) { + faceted_umap <- faceted_umap + + theme( + legend.position = "none", + aspect.ratio = 1 + ) + } else if (n_celltypes %% 3 != 0 & n_celltypes > 2) { faceted_umap <- faceted_umap + theme( legend.position = c(0.68, 0.33), @@ -365,13 +371,13 @@ All other cell types are grouped together and labeled "All remaining cell types" ```{r} -# Function to determine plot height, and width should sort itself out? -determine_umap_height <- function(n_types) { +# Helper function to determine UMAP plot height based on the number of celltypes +determine_umap_height <- function(n_celltypes) { dplyr::case_when( - n_types <= 2 ~ 3, - n_types <= 3 ~ 4, - n_types <= 5 ~ 6, - n_types <= 6 ~ 7, + n_celltypes == 1 ~ 3.5, + n_celltypes <= 3 ~ 4, + n_celltypes <= 5 ~ 6, + n_celltypes <= 6 ~ 7, .default = 8 ) } From cf16bfb17f235639e6439c47a20c8efefa109141 Mon Sep 17 00:00:00 2001 From: "Stephanie J. Spielman" Date: Mon, 8 Jan 2024 13:07:48 -0500 Subject: [PATCH 11/17] Add comments about legend placement choices --- templates/qc_report/celltypes_qc.rmd | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/templates/qc_report/celltypes_qc.rmd b/templates/qc_report/celltypes_qc.rmd index c3847052..9cd510eb 100644 --- a/templates/qc_report/celltypes_qc.rmd +++ b/templates/qc_report/celltypes_qc.rmd @@ -172,12 +172,17 @@ faceted_umap <- function(umap_df, # Determine legend placement based on total_celltypes if (n_celltypes == 1) { + # If n=1, we do not need to a show a legend faceted_umap <- faceted_umap + theme( legend.position = "none", aspect.ratio = 1 ) } else if (n_celltypes %% 3 != 0 & n_celltypes > 2) { + # This condition places the legend in the bottom right corner. Can use this for + # any n that doesn't have a full 3 columns, with one exception: for n=2, + # there will be no third column in the faceting to slot the legend into, + # so its legend has to go on the bottom faceted_umap <- faceted_umap + theme( legend.position = c(0.68, 0.33), @@ -189,6 +194,7 @@ faceted_umap <- function(umap_df, aspect.ratio = 1 ) } else { + # For any other n, place legend on the bottom faceted_umap <- faceted_umap + theme( legend.position = "bottom", From 61d22c200c9f21a703ba8f2ab5b288272aa5e3ac Mon Sep 17 00:00:00 2001 From: "Stephanie J. Spielman" Date: Mon, 8 Jan 2024 13:58:21 -0500 Subject: [PATCH 12/17] Styling: Align UMAPs in the center and update sizing approach to determine both width and height together --- templates/qc_report/celltypes_qc.rmd | 32 ++++++++++++++++------------ 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/templates/qc_report/celltypes_qc.rmd b/templates/qc_report/celltypes_qc.rmd index 9cd510eb..33b45a42 100644 --- a/templates/qc_report/celltypes_qc.rmd +++ b/templates/qc_report/celltypes_qc.rmd @@ -172,10 +172,11 @@ faceted_umap <- function(umap_df, # Determine legend placement based on total_celltypes if (n_celltypes == 1) { - # If n=1, we do not need to a show a legend + # If n=1, we do not need to a show a legend, and title should be smaller faceted_umap <- faceted_umap + theme( legend.position = "none", + plot.title = element_text(size = rel(0.85)), aspect.ratio = 1 ) } else if (n_celltypes %% 3 != 0 & n_celltypes > 2) { @@ -191,6 +192,7 @@ faceted_umap <- function(umap_df, # use slightly smaller legend text, which helps legend fit and prevents # long wrapped labels from bunching up legend.text = element_text(size = rel(0.85)), + legend.key.height = unit(0.75, "cm"), aspect.ratio = 1 ) } else { @@ -377,24 +379,26 @@ All other cell types are grouped together and labeled "All remaining cell types" ```{r} -# Helper function to determine UMAP plot height based on the number of celltypes -determine_umap_height <- function(n_celltypes) { +# Helper function to determine UMAP plot determine_umap_dimensions based on the number of celltypes +# returns a vector of c(width, height) +determine_umap_dimensions <- function(n_celltypes) { dplyr::case_when( - n_celltypes == 1 ~ 3.5, - n_celltypes <= 3 ~ 4, - n_celltypes <= 5 ~ 6, - n_celltypes <= 6 ~ 7, - .default = 8 + n_celltypes == 1 ~ c(3.75, 3.5), # 1: no legend + n_celltypes == 2 ~ c(5.5, 5), # 2: bottom legend + n_celltypes == 3 ~ c(8, 5.5), # 3: bottom legend + n_celltypes <= 5 ~ c(8, 7), # 4 & 5: inset legend + n_celltypes <= 6 ~ c(8, 7.5), # 6: bottom legend + .default = c(10, 9.5) # 8 & 9: inset legend ) } ``` ```{r, eval = has_submitter && has_umap} submitter_n_celltypes <- length(levels(umap_df$submitter_celltype_annotation_lumped)) -submitter_height <- determine_umap_height(submitter_n_celltypes) +submitter_dims <- determine_umap_dimensions(submitter_n_celltypes) ``` -```{r, eval = has_submitter && has_umap, message=FALSE, warning=FALSE, fig.height = submitter_height, fig.width = 8} +```{r, eval = has_submitter && has_umap, message=FALSE, warning=FALSE, fig.width = submitter_dims[1], fig.height = submitter_dims[2], fig.align = "center"} # umap for cell assign annotations faceted_umap( umap_df, @@ -407,11 +411,11 @@ faceted_umap( ```{r, eval = has_singler && has_umap} singler_n_celltypes <- length(levels(umap_df$singler_celltype_annotation_lumped)) -singler_height <- determine_umap_height(singler_n_celltypes) +singler_dims <- determine_umap_dimensions(singler_n_celltypes) ``` -```{r, eval=has_singler && has_umap, message=FALSE, warning=FALSE, fig.height = singler_height, fig.width = 8} +```{r, eval=has_singler && has_umap, message=FALSE, warning=FALSE, fig.width = singler_dims[1], fig.height = singler_dims[2], fig.align = "center"} # umap for cell assign annotations faceted_umap( umap_df, @@ -424,10 +428,10 @@ faceted_umap( ```{r, eval = has_cellassign && has_umap} cellassign_n_celltypes <- length(levels(umap_df$cellassign_celltype_annotation_lumped)) -cellassign_height <- determine_umap_height(cellassign_n_celltypes) +cellassign_dims <- determine_umap_dimensions(cellassign_n_celltypes) ``` -```{r, eval = has_cellassign && has_umap, message=FALSE, warning=FALSE, fig.height = cellassign_height, fig.width = 8} +```{r, eval = has_cellassign && has_umap, message=FALSE, warning=FALSE, fig.width = cellassign_dims[1], fig.height = cellassign_dims[2], fig.align = "center"} # umap for cell assign annotations faceted_umap( umap_df, From 2e040da889bfb0e33f00b2f878fe5bd6951bd289 Mon Sep 17 00:00:00 2001 From: "Stephanie J. Spielman" Date: Mon, 8 Jan 2024 13:59:53 -0500 Subject: [PATCH 13/17] use roxygen comments and put function in a function chunk higher in rmd --- templates/qc_report/celltypes_qc.rmd | 31 ++++++++++++++-------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/templates/qc_report/celltypes_qc.rmd b/templates/qc_report/celltypes_qc.rmd index 33b45a42..19c7b2c5 100644 --- a/templates/qc_report/celltypes_qc.rmd +++ b/templates/qc_report/celltypes_qc.rmd @@ -208,6 +208,22 @@ faceted_umap <- function(umap_df, return(faceted_umap) } + +#' Determine dimension for UMAP plot display based on number of cell types +#' +#' @param n_celltypes Number of cell types (here, facets) displayed in plot +#' +#' @return A vector of c(width, height) in inches +determine_umap_dimensions <- function(n_celltypes) { + dplyr::case_when( + n_celltypes == 1 ~ c(3.75, 3.5), # 1: no legend + n_celltypes == 2 ~ c(5.5, 5), # 2: bottom legend + n_celltypes == 3 ~ c(8, 5.5), # 3: bottom legend + n_celltypes <= 5 ~ c(8, 7), # 4 & 5: inset legend + n_celltypes <= 6 ~ c(8, 7.5), # 6: bottom legend + .default = c(10, 9.5) # 8 & 9: inset legend + ) +} ``` @@ -378,21 +394,6 @@ All other cell types are grouped together and labeled "All remaining cell types" -```{r} -# Helper function to determine UMAP plot determine_umap_dimensions based on the number of celltypes -# returns a vector of c(width, height) -determine_umap_dimensions <- function(n_celltypes) { - dplyr::case_when( - n_celltypes == 1 ~ c(3.75, 3.5), # 1: no legend - n_celltypes == 2 ~ c(5.5, 5), # 2: bottom legend - n_celltypes == 3 ~ c(8, 5.5), # 3: bottom legend - n_celltypes <= 5 ~ c(8, 7), # 4 & 5: inset legend - n_celltypes <= 6 ~ c(8, 7.5), # 6: bottom legend - .default = c(10, 9.5) # 8 & 9: inset legend - ) -} -``` - ```{r, eval = has_submitter && has_umap} submitter_n_celltypes <- length(levels(umap_df$submitter_celltype_annotation_lumped)) submitter_dims <- determine_umap_dimensions(submitter_n_celltypes) From 13dd624c7bfa01363f69f586a3cd9671541fc1ad Mon Sep 17 00:00:00 2001 From: "Stephanie J. Spielman" Date: Mon, 8 Jan 2024 14:41:40 -0500 Subject: [PATCH 14/17] fix comment typo --- templates/qc_report/celltypes_qc.rmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/qc_report/celltypes_qc.rmd b/templates/qc_report/celltypes_qc.rmd index 19c7b2c5..ee0ee533 100644 --- a/templates/qc_report/celltypes_qc.rmd +++ b/templates/qc_report/celltypes_qc.rmd @@ -221,7 +221,7 @@ determine_umap_dimensions <- function(n_celltypes) { n_celltypes == 3 ~ c(8, 5.5), # 3: bottom legend n_celltypes <= 5 ~ c(8, 7), # 4 & 5: inset legend n_celltypes <= 6 ~ c(8, 7.5), # 6: bottom legend - .default = c(10, 9.5) # 8 & 9: inset legend + .default = c(10, 9.5) # 7 & 8: inset legend ) } ``` From c40a52bc77bfadc0a04d6fd3415b239fb6bbccb1 Mon Sep 17 00:00:00 2001 From: Stephanie Spielman Date: Tue, 9 Jan 2024 10:50:33 -0500 Subject: [PATCH 15/17] Update templates/qc_report/celltypes_qc.rmd Co-authored-by: Ally Hawkins <54039191+allyhawkins@users.noreply.github.com> --- templates/qc_report/celltypes_qc.rmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/qc_report/celltypes_qc.rmd b/templates/qc_report/celltypes_qc.rmd index ee0ee533..d0eea509 100644 --- a/templates/qc_report/celltypes_qc.rmd +++ b/templates/qc_report/celltypes_qc.rmd @@ -219,7 +219,7 @@ determine_umap_dimensions <- function(n_celltypes) { n_celltypes == 1 ~ c(3.75, 3.5), # 1: no legend n_celltypes == 2 ~ c(5.5, 5), # 2: bottom legend n_celltypes == 3 ~ c(8, 5.5), # 3: bottom legend - n_celltypes <= 5 ~ c(8, 7), # 4 & 5: inset legend + n_celltypes <= 5 ~ c(9, 6), # 4 & 5: inset legend n_celltypes <= 6 ~ c(8, 7.5), # 6: bottom legend .default = c(10, 9.5) # 7 & 8: inset legend ) From 900a8d2d3abfb85bf4c5748f4227e45544c55514 Mon Sep 17 00:00:00 2001 From: Stephanie Spielman Date: Tue, 9 Jan 2024 10:50:39 -0500 Subject: [PATCH 16/17] Update templates/qc_report/celltypes_qc.rmd Co-authored-by: Ally Hawkins <54039191+allyhawkins@users.noreply.github.com> --- templates/qc_report/celltypes_qc.rmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/qc_report/celltypes_qc.rmd b/templates/qc_report/celltypes_qc.rmd index d0eea509..4ad0c2d6 100644 --- a/templates/qc_report/celltypes_qc.rmd +++ b/templates/qc_report/celltypes_qc.rmd @@ -221,7 +221,7 @@ determine_umap_dimensions <- function(n_celltypes) { n_celltypes == 3 ~ c(8, 5.5), # 3: bottom legend n_celltypes <= 5 ~ c(9, 6), # 4 & 5: inset legend n_celltypes <= 6 ~ c(8, 7.5), # 6: bottom legend - .default = c(10, 9.5) # 7 & 8: inset legend + .default = c(9, 9) # 7 & 8: inset legend ) } ``` From 16ffe3e18589138c7149e9f7ecca0fa5a7b02a60 Mon Sep 17 00:00:00 2001 From: "Stephanie J. Spielman" Date: Tue, 9 Jan 2024 12:07:33 -0500 Subject: [PATCH 17/17] Legend y-positioning based on number of facets --- templates/qc_report/celltypes_qc.rmd | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/templates/qc_report/celltypes_qc.rmd b/templates/qc_report/celltypes_qc.rmd index 4ad0c2d6..55c83515 100644 --- a/templates/qc_report/celltypes_qc.rmd +++ b/templates/qc_report/celltypes_qc.rmd @@ -135,6 +135,13 @@ plot_umap <- function( faceted_umap <- function(umap_df, n_celltypes, annotation_column) { + # Determine legend y-coordinate based on n_celltypes + if (n_celltypes %in% 7:8) { + legend_y <- 0.33 + } else if (n_celltypes %in% 4:5) { + legend_y <- 0.52 + } + # color by the annotation column but only color one cell type at a time faceted_umap <- ggplot( umap_df, @@ -186,7 +193,7 @@ faceted_umap <- function(umap_df, # so its legend has to go on the bottom faceted_umap <- faceted_umap + theme( - legend.position = c(0.68, 0.33), + legend.position = c(0.67, legend_y), legend.justification = c("left", "top"), legend.title.align = 0.5, # use slightly smaller legend text, which helps legend fit and prevents