Maps colours to variables, returning a matrix of RGB(A) values

colour_values_rgb(x, palette = "viridis", na_colour = "#808080FF",
  alpha = 255, include_alpha = TRUE, ...)

color_values_rgb(x, palette = "viridis", na_colour = "#808080FF",
  alpha = 255, include_alpha = TRUE, ...)

# S3 method for character
colour_values_to_rgb(x, palette, na_colour, alpha,
  include_alpha, summary = FALSE)

# S3 method for default
colour_values_to_rgb(x, palette, na_colour, alpha,
  include_alpha, n_summaries = 0, format = TRUE, digits = 2)

# S3 method for logical
colour_values_to_rgb(x, palette, na_colour, alpha,
  include_alpha, summary = FALSE)

# S3 method for factor
colour_values_to_rgb(x, palette, na_colour, alpha,
  include_alpha, summary = FALSE)

# S3 method for Date
colour_values_to_rgb(x, palette, na_colour, alpha,
  include_alpha, n_summaries = 0, format = TRUE)

# S3 method for POSIXct
colour_values_to_rgb(x, palette, na_colour, alpha,
  include_alpha, n_summaries = 0, format = TRUE)

# S3 method for POSIXlt
colour_values_to_rgb(x, palette, na_colour, alpha,
  include_alpha, n_summaries = 0, format = TRUE)

Arguments

x

vector of values to map to a colour

palette

colour palette. See details and examples

na_colour

hex string colour to use for NA values in the form #RRGGBBAA.

alpha

optional. Single value in [0,255] applied to all colours, or a decimal in [0, 1) (to indicate a percentage, noting 1 is excluded), or a vector of numeric values the same length as x. The numeric vector will be scaled into the range [0,255]. If a matrix palette is supplied this argument is ignored.

include_alpha

logical indicating if the returned hex or matrix should include the alpha values. Defaults to TRUE.

...

other arguments possed to methods

summary

logical indicating if a summary of the colours should be returned as well as the full colour mapping. This will be the unique elements of x mapped to the colour.

n_summaries

positive integer. If supplied a summary colour palette will be returned in a list, containing n_summaries equally spaced values of x in the range [min(x),max(x)], and their associated colours. If a non-numeric x is used this value is ignored

format

logical indicating if the summary values should be formatted. See details

digits

Integer. When summarising a numeric vector you can specify the number of decimal places to include in the summary values

Details

The palette can either be

  • String - use colour_palettes() to view available palettes

  • Matrix - At least 5 rows, and 3 (or 4) columns representing the red, green and blue (and alpha) values

The matrix palette requires 5 rows because the colours are interpolated using a cubic b-spline. This method requires 5 values.

when summary = TRUE, the following rules are applied to the summary values

  • logical vectors are converted to "TRUE" or "FALSE" strings

  • all other types remain as-is, unless format = T is used

when format = TRUE,

  • numbers are converted to strings with the specified number of decimal places (using digits argument)

  • Dates are formatted as "yyyy-mm-dd"

See also

colour_values

Examples

colour_values_rgb(1:5)
#> [,1] [,2] [,3] [,4] #> [1,] 68 1 84 255 #> [2,] 59 82 139 255 #> [3,] 33 144 140 255 #> [4,] 93 201 99 255 #> [5,] 253 231 37 255
colour_values_rgb(1:5, include_alpha = FALSE)
#> [,1] [,2] [,3] #> [1,] 68 1 84 #> [2,] 59 82 139 #> [3,] 33 144 140 #> [4,] 93 201 99 #> [5,] 253 231 37
colour_values_rgb(-25:25, n_summaries = 5)
#> $colours #> [,1] [,2] [,3] [,4] #> [1,] 68 1 84 255 #> [2,] 70 8 92 255 #> [3,] 71 16 99 255 #> [4,] 72 23 106 255 #> [5,] 72 30 112 255 #> [6,] 72 37 117 255 #> [7,] 71 43 122 255 #> [8,] 70 49 126 255 #> [9,] 69 56 130 255 #> [10,] 67 62 133 255 #> [11,] 65 68 135 255 #> [12,] 62 73 137 255 #> [13,] 60 79 139 255 #> [14,] 57 85 140 255 #> [15,] 55 90 140 255 #> [16,] 52 95 141 255 #> [17,] 50 100 142 255 #> [18,] 48 105 142 255 #> [19,] 46 110 142 255 #> [20,] 44 115 142 255 #> [21,] 42 120 142 255 #> [22,] 40 125 142 255 #> [23,] 38 130 142 255 #> [24,] 36 135 142 255 #> [25,] 34 139 141 255 #> [26,] 33 144 140 255 #> [27,] 31 149 139 255 #> [28,] 31 154 138 255 #> [29,] 31 158 136 255 #> [30,] 32 163 134 255 #> [31,] 34 168 132 255 #> [32,] 38 173 129 255 #> [33,] 44 177 126 255 #> [34,] 51 182 122 255 #> [35,] 58 186 118 255 #> [36,] 67 191 113 255 #> [37,] 77 195 108 255 #> [38,] 87 199 102 255 #> [39,] 98 202 95 255 #> [40,] 110 206 88 255 #> [41,] 122 209 81 255 #> [42,] 134 213 73 255 #> [43,] 147 215 65 255 #> [44,] 161 218 56 255 #> [45,] 174 220 48 255 #> [46,] 188 223 39 255 #> [47,] 201 224 31 255 #> [48,] 215 226 26 255 #> [49,] 228 228 24 255 #> [50,] 241 229 29 255 #> [51,] 253 231 37 255 #> #> $summary_values #> [1] "-25.00" "-12.50" "0.00" "12.50" "25.00" #> #> $summary_colours #> [,1] [,2] [,3] [,4] #> [1,] 68 1 84 255 #> [2,] 59 82 139 255 #> [3,] 33 144 140 255 #> [4,] 93 201 99 255 #> [5,] 253 231 37 255 #>