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