Skip to content

Commit

Permalink
Allow >=3 colors in color_scale, center breaks around 0, add color_sc…
Browse files Browse the repository at this point in the history
…ale example. Default matches original PRGn colors
  • Loading branch information
lahuuki committed Dec 16, 2024
1 parent cc78e20 commit 80e8845
Showing 1 changed file with 34 additions and 10 deletions.
44 changes: 34 additions & 10 deletions R/layer_stat_cor_plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
#'
Expand All @@ -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,
Expand All @@ -102,17 +108,35 @@ 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,
...) {
## 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)))
Expand Down

0 comments on commit 80e8845

Please sign in to comment.