-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathcontinuous-gsea.R
117 lines (109 loc) · 3.59 KB
/
continuous-gsea.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#' The GSEA GenePattern color palettes
#'
#' Color palette inspired by the colors used in the
#' heatmaps plotted by GSEA GenePattern.
#'
#' @param palette Palette type.
#' Currently there is one available option: `"default"`
#' (continuous palette with 12 base colors).
#' @param n Number of individual colors to be generated.
#' @param alpha Transparency level, a real number in (0, 1].
#' See `alpha` in [grDevices::rgb()] for details.
#' @param reverse Logical. Should the order of the colors be reversed?
#'
#' @export rgb_gsea
#'
#' @importFrom grDevices colorRamp rgb
#' @importFrom scales manual_pal
#'
#' @author Nan Xiao | \email{[email protected]} | <https://nanx.me>
#'
#' @note The 12 base colors used in this palette are derived from the
#' [HeatMapImage documentation](https://modulerepository.genepattern.org/gpModuleRepository/download/prod/module/?file=/HeatMapImage/broad.mit.edu:cancer.software.genepattern.module.analysis/00032/6/HeatMapImage.pdf).
#'
#' @examples
#' library("scales")
#' show_col(pal_gsea("default")(12))
#' show_col(pal_gsea("default", n = 30, alpha = 0.6, reverse = TRUE)(30))
rgb_gsea <- function(palette = c("default"), n = 12, alpha = 1, reverse = FALSE) {
palette <- match.arg(palette)
if (alpha > 1L || alpha <= 0L) stop("alpha must be in (0, 1]")
raw_cols <- ggsci_db$"gsea"[[palette]]
func_cols <- colorRamp(raw_cols, space = "Lab", interpolate = "spline")
mat_cols <- func_cols(seq(0L, 1L, length.out = n))
alpha_cols <- rgb(
mat_cols[, 1L], mat_cols[, 2L], mat_cols[, 3L],
alpha = alpha * 255L, maxColorValue = 255L
)
if (reverse) alpha_cols <- rev(alpha_cols)
alpha_cols
}
#' The GSEA GenePattern color palettes
#'
#' Color palette inspired by the colors used in the
#' heatmaps plotted by GSEA GenePattern.
#'
#' @inheritParams rgb_gsea
#'
#' @export pal_gsea
#'
#' @importFrom scales manual_pal
#'
#' @author Nan Xiao | \email{[email protected]} | <https://nanx.me>
#'
#' @examples
#' library("scales")
#' show_col(pal_gsea("default")(12))
#' show_col(pal_gsea("default", n = 30, alpha = 0.6, reverse = TRUE)(30))
pal_gsea <- function(palette = c("default"), n = 12, alpha = 1, reverse = FALSE) {
palette <- match.arg(palette)
alpha_cols <- rgb_gsea(palette, n, alpha, reverse)
manual_pal(unname(alpha_cols))
}
#' The GSEA GenePattern color scales
#'
#' See [pal_gsea()] for details.
#'
#' @inheritParams pal_gsea
#' @param ... Additional parameters for [ggplot2::discrete_scale()].
#'
#' @export scale_color_gsea
#'
#' @importFrom ggplot2 scale_color_gradientn
#'
#' @author Nan Xiao | \email{[email protected]} | <https://nanx.me>
#'
#' @rdname scale_gsea
#'
#' @examples
#' library("ggplot2")
#'
#' data("mtcars")
#' cor <- cor(mtcars)
#' cor_melt <- data.frame(
#' Var1 = rep(seq_len(nrow(cor)), times = ncol(cor)),
#' Var2 = rep(seq_len(ncol(cor)), each = nrow(cor)),
#' value = as.vector(cor)
#' )
#'
#' ggplot(
#' cor_melt,
#' aes(x = Var1, y = Var2, fill = value)
#' ) +
#' geom_tile(colour = "black", size = 0.3) +
#' theme_bw() +
#' scale_fill_gsea()
scale_color_gsea <- function(palette = c("default"), alpha = 1, reverse = FALSE, ...) {
palette <- match.arg(palette)
scale_color_gradientn(colours = rgb_gsea(palette, n = 512, alpha = alpha, reverse = reverse), ...)
}
#' @export scale_colour_gsea
#' @rdname scale_gsea
scale_colour_gsea <- scale_color_gsea
#' @export scale_fill_gsea
#' @importFrom ggplot2 scale_fill_gradientn
#' @rdname scale_gsea
scale_fill_gsea <- function(palette = c("default"), alpha = 1, reverse = FALSE, ...) {
palette <- match.arg(palette)
scale_fill_gradientn(colours = rgb_gsea(palette, n = 512, alpha = alpha, reverse = reverse), ...)
}