From 80e8845eb179d7129efbcbcc64af492c8e33ec57 Mon Sep 17 00:00:00 2001 From: Louise Huuki Date: Mon, 16 Dec 2024 16:40:33 -0500 Subject: [PATCH 1/2] Allow >=3 colors in color_scale, center breaks around 0, add color_scale example. Default matches original PRGn colors --- R/layer_stat_cor_plot.R | 44 +++++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/R/layer_stat_cor_plot.R b/R/layer_stat_cor_plot.R index edefd095..9f4d85eb 100644 --- a/R/layer_stat_cor_plot.R +++ b/R/layer_stat_cor_plot.R @@ -16,9 +16,10 @@ #' the color scale (should be between 0 and 1). #' @param color_min A `numeric(1)` specifying the lowest correlation value for #' the color scale (should be between 0 and -1). -#' @param color_scale A `character(3)` vector specifying the color scale for the -#' fill of the heatmap. The first value is used for `color_min`, the second one -#' for zero, and the third for `color_max`. +#' @param color_scale A `character` vector with three or more values specifying +#' the color scale for the fill of the heatmap. The first value is used for +#' `color_min`, the middle for zero, and the last for `color_max`. If an even +#' number of colors are supplied, the last color is dropped to center zero. #' @param query_colors named `character` vector of colors, Adds colors to query #' row annotations. #' @param reference_colors named `character` vector of colors, Adds colors to @@ -64,7 +65,7 @@ #' ## Default plot with no annotations and defaults for ComplexHeatmap() #' layer_stat_cor_plot(cor_stats_layer) #' -#' ## add colors +#' ## add Annotation colors #' ## add libd_layer_colors to reference Human Pilot layers #' layer_stat_cor_plot(cor_stats_layer, reference_colors = libd_layer_colors) #' @@ -87,10 +88,15 @@ #' confidence_threshold = .55 #' ) #' layer_stat_cor_plot(cor_stats_layer, annotation = annotation_df) -#' +#' +#' ## change fill color scale +#' layer_stat_cor_plot(cor_stats_layer, +#' color_scale = RColorBrewer::brewer.pal(2, "PiYG")) +#' #' ## All together #' layer_stat_cor_plot( -#' cor_stats_layer, +#' cor_stats_layer, +#' color_scale = RColorBrewer::brewer.pal(5, "PiYG"), #' query_colors = cluster_colors, #' reference_colors = libd_layer_colors, #' annotation = annotation_df, @@ -102,7 +108,7 @@ layer_stat_cor_plot <- function( cor_stats_layer, color_max = max(cor_stats_layer), color_min = min(cor_stats_layer), - color_scale = c("#762A83", "#F7F7F7", "#1B7837"), + color_scale = RColorBrewer::brewer.pal(7, "PRGn"), query_colors = NULL, reference_colors = NULL, annotation = NULL, @@ -110,9 +116,27 @@ layer_stat_cor_plot <- function( ## define color pallet stopifnot(color_min < color_max) stopifnot(color_min < 0) - stopifnot(length(color_scale) == 3) - my.col <- circlize::colorRamp2(c(color_min, 0, color_max), color_scale) - + stopifnot(length(color_scale) >= 3) + # my.col <- circlize::colorRamp2(c(color_min, 0, color_max), color_scale) + + # create a sequence from color_min to color max centered around 0 + n.col <- length(color_scale) + zero_center_seq <- unique(c(seq(color_min, 0, length.out = ceiling(n.col/2)), + seq(0, color_max, length.out = ceiling(n.col/2)))) + + if(!length(color_scale) == length(zero_center_seq)){ + warning(sprintf("Using %d/%d colors to center zero, dropping %s", + length(zero_center_seq), + n.col, + color_scale[n.col]), call. = FALSE) + color_scale <- color_scale[seq(length(zero_center_seq))] + } + + my.col <- circlize::colorRamp2( + breaks = zero_center_seq, + colors = color_scale + ) + # ## query annotations on row if (!is.null(query_colors)) { stopifnot(all(rownames(cor_stats_layer) %in% names(query_colors))) From 7c7acabcb22f888bf4fe613b9a52601e9e21a2e6 Mon Sep 17 00:00:00 2001 From: Louise Huuki Date: Mon, 16 Dec 2024 16:41:47 -0500 Subject: [PATCH 2/2] Update layer_stat_cor_plot docs with color_scale upgrades --- man/layer_stat_cor_plot.Rd | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/man/layer_stat_cor_plot.Rd b/man/layer_stat_cor_plot.Rd index 835ceb4e..cae88c70 100644 --- a/man/layer_stat_cor_plot.Rd +++ b/man/layer_stat_cor_plot.Rd @@ -8,7 +8,7 @@ layer_stat_cor_plot( cor_stats_layer, color_max = max(cor_stats_layer), color_min = min(cor_stats_layer), - color_scale = c("#762A83", "#F7F7F7", "#1B7837"), + color_scale = RColorBrewer::brewer.pal(7, "PRGn"), query_colors = NULL, reference_colors = NULL, annotation = NULL, @@ -24,9 +24,10 @@ the color scale (should be between 0 and 1).} \item{color_min}{A \code{numeric(1)} specifying the lowest correlation value for the color scale (should be between 0 and -1).} -\item{color_scale}{A \code{character(3)} vector specifying the color scale for the -fill of the heatmap. The first value is used for \code{color_min}, the second one -for zero, and the third for \code{color_max}.} +\item{color_scale}{A \code{character} vector with three or more values specifying +the color scale for the fill of the heatmap. The first value is used for +\code{color_min}, the middle for zero, and the last for \code{color_max}. If an even +number of colors are supplied, the last color is dropped to center zero.} \item{query_colors}{named \code{character} vector of colors, Adds colors to query row annotations.} @@ -83,7 +84,7 @@ cor_stats_layer <- layer_stat_cor( ## Default plot with no annotations and defaults for ComplexHeatmap() layer_stat_cor_plot(cor_stats_layer) -## add colors +## add Annotation colors ## add libd_layer_colors to reference Human Pilot layers layer_stat_cor_plot(cor_stats_layer, reference_colors = libd_layer_colors) @@ -107,9 +108,14 @@ annotation_df <- annotate_registered_clusters( ) layer_stat_cor_plot(cor_stats_layer, annotation = annotation_df) +## change fill color scale +layer_stat_cor_plot(cor_stats_layer, + color_scale = RColorBrewer::brewer.pal(2, "PiYG")) + ## All together layer_stat_cor_plot( - cor_stats_layer, + cor_stats_layer, + color_scale = RColorBrewer::brewer.pal(5, "PiYG"), query_colors = cluster_colors, reference_colors = libd_layer_colors, annotation = annotation_df,