View source: R/scale-unbinned.R
as.discretised_scale | R Documentation |
This scale allows ggplot to understand data that has been discretised with
some procedure akin to cut
and access the underlying continuous values.
For a scale that does the opposite (take continuous data and treat them as
discrete) see ggplot2::binned_scale()
.
as.discretised_scale(scale_function)
scale_fill_discretised(
...,
low = "#132B43",
high = "#56B1F7",
space = "Lab",
na.value = "grey50",
guide = ggplot2::guide_colorsteps(even.steps = FALSE, show.limits = TRUE),
aesthetics = "fill"
)
scale_fill_divergent_discretised(
...,
low = scales::muted("blue"),
mid = "white",
high = scales::muted("red"),
midpoint = 0,
space = "Lab",
na.value = "grey50",
guide = ggplot2::guide_colorsteps(even.steps = FALSE, show.limits = TRUE)
)
discretised_scale(
aesthetics,
scale_name,
palette,
name = ggplot2::waiver(),
breaks = ggplot2::waiver(),
labels = ggplot2::waiver(),
limits = NULL,
trans = scales::identity_trans(),
na.value = NA,
drop = FALSE,
guide = ggplot2::guide_colorsteps(even.steps = FALSE),
position = "left",
rescaler = scales::rescale,
oob = scales::censor,
super = ScaleDiscretised
)
This scale makes it very easy to synchronise the breaks of filled contours
and the breaks shown no the colour guide. Bear in mind that when using
geom_contour_fill()
, the default fill aesthetic (level_mid
) is not
discretised. To use this scale with that geom, you need to set
aes(fill = after_stat(level))
.
A function with the same arguments as scale_function
that works with discretised
values.
scale_fill_discretised
library(ggplot2)
scale_fill_brewer_discretised <- as.discretised_scale(scale_fill_distiller)
library(ggplot2)
# Using the `level` compute aesthetic from `geom_contour_fill()`
# (or ggplot2::geom_contour_filled()), the default scale is discrete.
# This means that you cannot map colours to the underlying numbers.
v <- ggplot(faithfuld, aes(waiting, eruptions, z = density))
v + geom_contour_fill(aes(fill = after_stat(level)))
v + geom_contour_fill(aes(fill = after_stat(level))) +
scale_fill_discretised()
# The scale can be customised the same as any continuous colour scale
v + geom_contour_fill(aes(fill = after_stat(level))) +
scale_fill_discretised(low = "#a62100", high = "#fff394")
# Setting limits explicitly will truncate the scale
# (if any limit is inside the range of the breaks but doesn't
# coincide with any range, it will be rounded with a warning)
v + geom_contour_fill(aes(fill = after_stat(level))) +
scale_fill_discretised(low = "#a62100", high = "#fff394",
limits = c(0.01, 0.028))
# Or extend it.
v + geom_contour_fill(aes(fill = after_stat(level))) +
scale_fill_discretised(low = "#a62100", high = "#fff394",
limits = c(0, 0.07))
v + geom_contour_fill(aes(fill = after_stat(level))) +
scale_fill_divergent_discretised(midpoint = 0.02)
# Existing continous scales can be "retrofitted" by changing the `super`
# and `guide` arguments.
v + geom_contour_fill(aes(fill = after_stat(level))) +
scale_fill_distiller(super = ScaleDiscretised)
# Unequal breaks will, by default, map to unequal spacing in the guide
v + geom_contour_fill(aes(fill = after_stat(level)), breaks = c(0, 0.005, 0.01, 0.02, 0.04)) +
scale_fill_discretised()
# You can change that by the `even.steps` argument on ggplot2::guide_colorsteps()
v + geom_contour_fill(aes(fill = after_stat(level)), breaks = c(0, 0.005, 0.01, 0.02, 0.04)) +
scale_fill_discretised(guide = guide_colorsteps(even.steps = TRUE, show.limits = TRUE))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.