Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add color_scale flexibility to layer_stat_cor_plot() #94

Open
wants to merge 2 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
18 changes: 12 additions & 6 deletions man/layer_stat_cor_plot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading