Skip to contents

Creates a categorical scale. Categorical scales map input values defined in the set of levels to colours (or values). Input values not in the set of levels are assigned unmapped_color (or unmapped_value).

Usage

scale_color_category(
  col,
  palette = scales::brewer_pal("div"),
  unmapped_color = "#000000",
  levels = NULL,
  unmapped_tick = NULL,
  tick_format = NULL,
  col_label = "{.col}",
  legend = TRUE
)

scale_category(
  col,
  range = 0:1,
  unmapped_value = 0,
  levels = NULL,
  col_label = "{.col}",
  legend = TRUE
)

Arguments

col

<name | string> The name of the column containing data to be scaled. Must be a valid input to rlang::ensym(); either a named column (non-standard evaluation), a string. Supports tidy-eval.

palette

<color|function> The colour palette of the colour scale. Must be a:

  • vector of RGBA hex colours,

  • a palette generator function, taking a length parameter, or

  • a palette ramp created from scales::colour_ramp()

A scales::colour_ramp() interpolator is created from the input palette.

unmapped_color

<color> The colour representing unmapped levels.

levels

<factor | character | logical> The category levels. If NULL, will be populated from input data. The order of the levels is determined by levels() for factors & unique() otherwise.

If there are more levels than colours (or range values), the palette (or range) is interpolated.

unmapped_tick

<string> The tick label of the unmapped category. If not NULL and legend == TRUE, the unmapped category will appear at the bottom of the legend.

tick_format

<function> A label function taking a vector of ticks returning formatted ticks.

col_label

<string | function> A template string or a label function for customising the column name in the legend.

  • if col_label is a string, {.col} may be used to represent the col name

  • if col_label is a function, the function must take a single argument: the col name

legend

<boolean> Indicate whether the legend should be displayed for this scale.

range

<numeric> The output range of the numeric scale. Must be length >= 2. A stats::approxfun() interpolator is created from the input range.

unmapped_value

<number> The value representing unmapped levels.