Skip to content

The default discrete colour scale. Defaults to scale_fill_hue()/scale_fill_brewer() unless type (which defaults to the ggplot2.discrete.fill/ggplot2.discrete.colour options) is specified.

Usage

scale_colour_discrete(
  ...,
  palette = NULL,
  aesthetics = "colour",
  na.value = "grey50",
  type = getOption("ggplot2.discrete.colour")
)

scale_fill_discrete(
  ...,
  palette = NULL,
  aesthetics = "fill",
  na.value = "grey50",
  type = getOption("ggplot2.discrete.fill")
)

Arguments

...

Additional parameters passed on to the scale type,

palette

One of the following:

  • NULL for the default palette stored in the theme.

  • a character vector of colours.

  • a single string naming a palette.

  • a palette function that when called with a single integer argument (the number of levels in the scale) returns the values that they should take.

aesthetics

The names of the aesthetics that this scale works with.

na.value

If na.translate = TRUE, what aesthetic value should the missing values be displayed as? Does not apply to position scales where NA is always placed at the far right.

type

One of the following:

  • A character vector of color codes. The codes are used for a 'manual' color scale as long as the number of codes exceeds the number of data levels (if there are more levels than codes, scale_colour_hue()/scale_fill_hue() are used to construct the default scale). If this is a named vector, then the color values will be matched to levels based on the names of the vectors. Data values that don't match will be set as na.value.

  • A list of character vectors of color codes. The minimum length vector that exceeds the number of data levels is chosen for the color scaling. This is useful if you want to change the color palette based on the number of levels.

  • A function that returns a discrete colour/fill scale (e.g., scale_fill_hue(), scale_fill_brewer(), etc).

See also

The discrete colour scales section of the online ggplot2 book.

Examples

# A standard plot
p <- ggplot(mpg, aes(displ, hwy, colour = class)) +
  geom_point()

# You can use the scale to give a palette directly
p + scale_colour_discrete(palette = scales::pal_brewer(palette = "Dark2"))


# The default colours are encoded into the theme
p + theme(palette.colour.discrete = scales::pal_grey())


# You can globally set default colour palette via the theme
old <- update_theme(palette.colour.discrete = scales::pal_viridis())

# Plot now shows new global default
p


# Restoring the previous theme
theme_set(old)