Expanding limits: expand_limits
Mix.install([
{:giggly, path: Path.join(__DIR__, "..")},
{:kino, "~> 0.19"}
])
About this notebook
This notebook follows the examples on ggplot2's reference page for
expand_limits(), in the order the
page gives them. Each example shows ggplot2's code first and then Giggly's. An example Giggly
cannot draw yet keeps its ggplot2 code and names what it is waiting for, so the page is also a list
of what remains.
expand_limits() widens a plot's scales to include the values it is given, in every panel, by
adding a layer that draws nothing at those values (a geom_blank()). ggplot2 now prefers giving a
scale's limits a function instead, such as scale_x_continuous(limits = ~range(.x, 0)) to include
zero, which ggplot2 draws exactly as it draws expand_limits(). Giggly has no expand_limits(),
since ggplot2 has superseded it, and its examples wait for a function as a scale's limits:
Giggly's limits are numbers or values, never a function, as Giggly.Plot.lims/2 and the scale
functions' limits: take them.
Including a value on an axis
p <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
p + expand_limits(x = 0)
p + expand_limits(y = c(1, 9))
p + expand_limits(x = 0, y = 0)
ggplot2 draws each the same with a function as the scale's limits, expand_limits(x = 0) as
scale_x_continuous(limits = ~range(.x, 0)).
Not in Giggly yet: a function as a scale's limits.
Widening a colour scale
The first plot widens a continuous colour scale to run from 2 to 10; the second adds 2, 4, 6, 8 and 10 to a discrete one, so its legend lists them all, though no car has 2 or 10 cylinders.
ggplot(mtcars, aes(mpg, wt)) +
geom_point(aes(colour = cyl)) +
expand_limits(colour = seq(2, 10, by = 2))
ggplot(mtcars, aes(mpg, wt)) +
geom_point(aes(colour = factor(cyl))) +
expand_limits(colour = factor(seq(2, 10, by = 2)))
ggplot2 draws each the same with a function as the colour scale's limits, the first as
scale_colour_continuous(limits = ~range(.x, seq(2, 10, by = 2))).
Not in Giggly yet: a function as a scale's limits.