The scales scale_colour_continuous()
and scale_fill_continuous()
are
the default colour scales ggplot2 uses when continuous data values are
mapped onto the colour
or fill
aesthetics, respectively. The scales
scale_colour_binned()
and scale_fill_binned()
are equivalent scale
functions that assign discrete color bins to the continuous values
instead of using a continuous color spectrum.
Usage
scale_colour_continuous(
...,
palette = NULL,
aesthetics = "colour",
guide = "colourbar",
na.value = "grey50",
type = getOption("ggplot2.continuous.colour")
)
scale_fill_continuous(
...,
palette = NULL,
aesthetics = "fill",
guide = "colourbar",
na.value = "grey50",
type = getOption("ggplot2.continuous.fill")
)
scale_colour_binned(
...,
palette = NULL,
aesthetics = "colour",
guide = "coloursteps",
na.value = "grey50",
type = getOption("ggplot2.binned.colour")
)
scale_fill_binned(
...,
palette = NULL,
aesthetics = "fill",
guide = "coloursteps",
na.value = "grey50",
type = getOption("ggplot2.binned.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 numeric vector with values between 0 and 1 returns the corresponding output values.
- aesthetics
The names of the aesthetics that this scale works with.
- guide
A function used to create a guide or its name. See
guides()
for more information.- na.value
Missing values will be replaced with this value.
- type
"gradient" (the default)
"viridis"
A function that returns a continuous colour scale.
Details
: The mechanism of setting defaults via
options()
is superseded by theme settings. The preferred method to change
the default palette of scales is via the theme, for example:
theme(palette.colour.continuous = scales::pal_viridis())
. The
ggplot2.continuous.colour
and ggplot2.continuous.fill
options could be
used to set default continuous scales and ggplot2.binned.colour
and
ggplot2.binned.fill
options to set default binned scales.
These scale functions are meant to provide simple defaults. If
you want to manually set the colors of a scale, consider using
scale_colour_gradient()
or scale_colour_steps()
.
Color Blindness
Many color palettes derived from RGB combinations (like the "rainbow" color
palette) are not suitable to support all viewers, especially those with
color vision deficiencies. Using viridis
type, which is perceptually
uniform in both colour and black-and-white display is an easy option to
ensure good perceptive properties of your visualizations.
The colorspace package offers functionalities
to generate color palettes with good perceptive properties,
to analyse a given color palette, like emulating color blindness,
and to modify a given color palette for better perceptivity.
For more information on color vision deficiencies and suitable color choices see the paper on the colorspace package and references therein.
See also
scale_colour_gradient()
, scale_colour_viridis_c()
,
scale_colour_steps()
, scale_colour_viridis_b()
, scale_fill_gradient()
,
scale_fill_viridis_c()
, scale_fill_steps()
, and scale_fill_viridis_b()
The documentation on colour aesthetics.
The continuous colour scales section of the online ggplot2 book.
Other colour scales:
scale_alpha()
,
scale_colour_brewer()
,
scale_colour_gradient()
,
scale_colour_grey()
,
scale_colour_hue()
,
scale_colour_identity()
,
scale_colour_manual()
,
scale_colour_steps()
,
scale_colour_viridis_d()
Examples
# A standard plot
p <- ggplot(mpg, aes(displ, hwy, colour = cty)) +
geom_point()
# You can use the scale to give a palette directly
p + scale_colour_continuous(palette = c("#FEE0D2", "#FC9272", "#DE2D26"))
# The default colours are encoded into the theme
p + theme(palette.colour.continuous = c("#DEEBF7", "#9ECAE1", "#3182BD"))
# You can globally set default colour palette via the theme
old <- update_theme(palette.colour.continuous = c("#E5F5E0", "#A1D99B", "#31A354"))
# Plot now shows new global default
p
# The default binned colour scale uses the continuous palette
p + scale_colour_binned() +
theme(palette.colour.continuous = c("#EFEDF5", "#BCBDDC", "#756BB1"))
# Restoring the previous theme
theme_set(old)