Skip to contents

Generate colors for ComplexHeatmap for continuous variables

Usage

format_colors_num(
  x,
  centered = TRUE,
  palette = NULL,
  rev = FALSE,
  trim_values = NULL,
  trim_prop = NULL,
  trim_range = NULL,
  ceiling = FALSE
)

Arguments

x

Vector or matrix of continuous values for which we want to specify colors.

centered

Logical. Default value TRUE.

palette

Vector of at least two colors used to create a color palette with 'colorRampPalette' or name of a RColorBrewer palette e.g. "Oranges", "Spectral".

Value

Vector of unique colors for all unique values of x.

Examples


x <- rnorm(20)

palette <- "Spectral"

format_colors_num(x, trim_values = 2.5)
#> function (x = NULL, return_rgb = FALSE, max_value = 1) 
#> {
#>     if (is.null(x)) {
#>         stop_wrap("Please specify `x`\n")
#>     }
#>     att = attributes(x)
#>     if (is.data.frame(x)) 
#>         x = as.matrix(x)
#>     l_na = is.na(x)
#>     if (all(l_na)) {
#>         return(rep(NA, length(l_na)))
#>     }
#>     x2 = x[!l_na]
#>     x2 = ifelse(x2 < breaks[1], breaks[1], ifelse(x2 > breaks[length(breaks)], 
#>         breaks[length(breaks)], x2))
#>     ibin = .bincode(x2, breaks, right = TRUE, include.lowest = TRUE)
#>     res_col = character(length(x2))
#>     for (i in unique(ibin)) {
#>         l = ibin == i
#>         res_col[l] = .get_color(x2[l], breaks[i], breaks[i + 
#>             1], colors[i, ], colors[i + 1, ], space = space)
#>     }
#>     res_col = paste(res_col, transparency_str[1], sep = "")
#>     if (return_rgb) {
#>         res_col = t(col2rgb(as.vector(res_col), alpha = TRUE)/255)
#>         return(res_col)
#>     }
#>     else {
#>         res_col2 = character(length(x))
#>         res_col2[l_na] = NA
#>         res_col2[!l_na] = res_col
#>         attributes(res_col2) = att
#>         return(res_col2)
#>     }
#> }
#> <bytecode: 0x7f7b06b4d7d8>
#> <environment: 0x7f7b06b3bdf0>
#> attr(,"breaks")
#>  [1] -2.5000000 -2.2058824 -1.9117647 -1.6176471 -1.3235294 -1.0294118
#>  [7] -0.7352941 -0.4411765 -0.1470588  0.1470588  0.4411765  0.7352941
#> [13]  1.0294118  1.3235294  1.6176471  1.9117647  2.2058824  2.5000000
#> attr(,"colors")
#>  [1] "#08306BFF" "#08519CFF" "#2171B5FF" "#4292C6FF" "#6BAED6FF" "#9ECAE1FF"
#>  [7] "#C6DBEFFF" "#DEEBF7FF" "#F7FBFFFF" "#FFF5EBFF" "#FEE6CEFF" "#FDD0A2FF"
#> [13] "#FDAE6BFF" "#FD8D3CFF" "#F16913FF" "#D94801FF" "#A63603FF" "#7F2704FF"
#> attr(,"transparency")
#> [1] 0
#> attr(,"space")
#> [1] "LAB"