Skip to content

Commit 459af8e

Browse files
authored
Merge pull request #94 from LieberInstitute/layer_stat_cor_plot_original_colors
Add color_scale flexibility to layer_stat_cor_plot()
2 parents 26fe43e + f77ffeb commit 459af8e

File tree

2 files changed

+54
-23
lines changed

2 files changed

+54
-23
lines changed

R/layer_stat_cor_plot.R

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
#' the color scale (should be between 0 and 1).
1717
#' @param color_min A `numeric(1)` specifying the lowest correlation value for
1818
#' the color scale (should be between 0 and -1).
19-
#' @param color_scale A `character(3)` vector specifying the color scale for the
20-
#' fill of the heatmap. The first value is used for `color_min`, the second one
21-
#' for zero, and the third for `color_max`.
19+
#' @param color_scale A `character` vector with three or more values specifying
20+
#' the color scale for the fill of the heatmap. The first value is used for
21+
#' `color_min`, the middle for zero, and the last for `color_max`. If an even
22+
#' number of colors are supplied, the last color is dropped to center zero.
2223
#' @param query_colors named `character` vector of colors, Adds colors to query
2324
#' row annotations.
2425
#' @param reference_colors named `character` vector of colors, Adds colors to
@@ -64,7 +65,7 @@
6465
#' ## Default plot with no annotations and defaults for ComplexHeatmap()
6566
#' layer_stat_cor_plot(cor_stats_layer)
6667
#'
67-
#' ## add colors
68+
#' ## add Annotation colors
6869
#' ## add libd_layer_colors to reference Human Pilot layers
6970
#' layer_stat_cor_plot(cor_stats_layer, reference_colors = libd_layer_colors)
7071
#'
@@ -87,31 +88,55 @@
8788
#' confidence_threshold = .55
8889
#' )
8990
#' layer_stat_cor_plot(cor_stats_layer, annotation = annotation_df)
90-
#'
91+
#'
92+
#' ## change fill color scale
93+
#' layer_stat_cor_plot(cor_stats_layer,
94+
#' color_scale = RColorBrewer::brewer.pal(2, "PiYG"))
95+
#'
9196
#' ## All together
9297
#' layer_stat_cor_plot(
93-
#' cor_stats_layer,
98+
#' cor_stats_layer,
99+
#' color_scale = RColorBrewer::brewer.pal(5, "PiYG"),
94100
#' query_colors = cluster_colors,
95101
#' reference_colors = libd_layer_colors,
96102
#' annotation = annotation_df,
97103
#' cluster_rows = FALSE,
98104
#' cluster_columns = FALSE
99105
#' )
100106
#'
101-
layer_stat_cor_plot <- function(cor_stats_layer,
102-
color_max = max(cor_stats_layer),
103-
color_min = min(cor_stats_layer),
104-
color_scale = c("#762A83", "#F7F7F7", "#1B7837"),
105-
query_colors = NULL,
106-
reference_colors = NULL,
107-
annotation = NULL,
108-
...) {
107+
layer_stat_cor_plot <- function(
108+
cor_stats_layer,
109+
color_max = max(cor_stats_layer),
110+
color_min = min(cor_stats_layer),
111+
color_scale = RColorBrewer::brewer.pal(7, "PRGn"),
112+
query_colors = NULL,
113+
reference_colors = NULL,
114+
annotation = NULL,
115+
...) {
109116
## define color pallet
110117
stopifnot(color_min < color_max)
111118
stopifnot(color_min < 0)
112-
stopifnot(length(color_scale) == 3)
113-
my.col <- circlize::colorRamp2(c(color_min, 0, color_max), color_scale)
114-
119+
stopifnot(length(color_scale) >= 3)
120+
# my.col <- circlize::colorRamp2(c(color_min, 0, color_max), color_scale)
121+
122+
# create a sequence from color_min to color max centered around 0
123+
n.col <- length(color_scale)
124+
zero_center_seq <- unique(c(seq(color_min, 0, length.out = ceiling(n.col/2)),
125+
seq(0, color_max, length.out = ceiling(n.col/2))))
126+
127+
if(!length(color_scale) == length(zero_center_seq)){
128+
warning(sprintf("Using %d/%d colors to center zero, dropping %s",
129+
length(zero_center_seq),
130+
n.col,
131+
color_scale[n.col]), call. = FALSE)
132+
color_scale <- color_scale[seq(length(zero_center_seq))]
133+
}
134+
135+
my.col <- circlize::colorRamp2(
136+
breaks = zero_center_seq,
137+
colors = color_scale
138+
)
139+
115140
# ## query annotations on row
116141
if (!is.null(query_colors)) {
117142
stopifnot(all(rownames(cor_stats_layer) %in% names(query_colors)))

man/layer_stat_cor_plot.Rd

Lines changed: 12 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)