|
16 | 16 | #' the color scale (should be between 0 and 1). |
17 | 17 | #' @param color_min A `numeric(1)` specifying the lowest correlation value for |
18 | 18 | #' 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. |
22 | 23 | #' @param query_colors named `character` vector of colors, Adds colors to query |
23 | 24 | #' row annotations. |
24 | 25 | #' @param reference_colors named `character` vector of colors, Adds colors to |
|
64 | 65 | #' ## Default plot with no annotations and defaults for ComplexHeatmap() |
65 | 66 | #' layer_stat_cor_plot(cor_stats_layer) |
66 | 67 | #' |
67 | | -#' ## add colors |
| 68 | +#' ## add Annotation colors |
68 | 69 | #' ## add libd_layer_colors to reference Human Pilot layers |
69 | 70 | #' layer_stat_cor_plot(cor_stats_layer, reference_colors = libd_layer_colors) |
70 | 71 | #' |
|
87 | 88 | #' confidence_threshold = .55 |
88 | 89 | #' ) |
89 | 90 | #' 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 | +#' |
91 | 96 | #' ## All together |
92 | 97 | #' layer_stat_cor_plot( |
93 | | -#' cor_stats_layer, |
| 98 | +#' cor_stats_layer, |
| 99 | +#' color_scale = RColorBrewer::brewer.pal(5, "PiYG"), |
94 | 100 | #' query_colors = cluster_colors, |
95 | 101 | #' reference_colors = libd_layer_colors, |
96 | 102 | #' annotation = annotation_df, |
97 | 103 | #' cluster_rows = FALSE, |
98 | 104 | #' cluster_columns = FALSE |
99 | 105 | #' ) |
100 | 106 | #' |
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 | + ...) { |
109 | 116 | ## define color pallet |
110 | 117 | stopifnot(color_min < color_max) |
111 | 118 | 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 | + |
115 | 140 | # ## query annotations on row |
116 | 141 | if (!is.null(query_colors)) { |
117 | 142 | stopifnot(all(rownames(cor_stats_layer) %in% names(query_colors))) |
|
0 commit comments