as.discretised_scale {metR} | R Documentation |
Create discretised versions of continuous scales
Description
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()
.
Usage
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
)
Arguments
scale_function |
a scale function (e.g. |
... |
Arguments passed on to
|
low , high |
Colours for low and high ends of the gradient. |
space |
colour space in which to calculate gradient. Must be "Lab" - other values are deprecated. |
na.value |
Colour to use for missing values |
guide |
Type of legend. Use |
aesthetics |
Character string or vector of character strings listing the
name(s) of the aesthetic(s) that this scale works with. This can be useful, for
example, to apply colour settings to the |
mid |
colour for mid point |
midpoint |
The midpoint (in data value) of the diverging scale. Defaults to 0. |
scale_name |
|
palette |
A palette function that when called with a numeric vector with
values between 0 and 1 returns the corresponding output values
(e.g., |
name |
The name of the scale. Used as the axis or legend title. If
|
breaks |
One of:
|
labels |
One of:
|
limits |
One of:
|
trans |
|
drop |
Should unused factor levels be omitted from the scale? The default, TRUE, uses the levels that appear in the data; FALSE uses all the levels in the factor. |
position |
For position scales, The position of the axis.
|
rescaler |
A function used to scale the input values to the
range [0, 1]. This is always |
oob |
One of:
|
super |
The super class to use for the constructed scale |
Details
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))
.
Value
A function with the same arguments as scale_function
that works with discretised
values.
See Also
scale_fill_discretised
Examples
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))