ggplot2
ggplot2 copied to clipboard
Feature request: flexible parametrisation of rectangles
Currently we have the following parametrisation for rectangles:
geom_tile()
has x
, y
, width
and height
.
geom_rect()
has xmin
, xmax
, ymin
and ymax
.
If you know two of the x
or y
related aesthetics, you can compute the rest regardless of which two are known. For example, if you know xmin
and width
, you can compute xmax = xmin + width
.
Being able to abitrarily pick two aesthetics with the others imputed allows for more flexibility. For example the following two options for visualising the presidential
dataset are both horrifying.
Using geom_rect()
option, you're forced to manually resolve a discrete y
into a continuous y
and hack a continuous scale to look like a discrete scale:
library(ggplot2)
ggplot(presidential) +
geom_rect(
aes(xmin = start,
xmax = end,
ymin = match(name, unique(name)) - 0.4,
ymax = match(name, unique(name)) + 0.4)) +
scale_y_continuous(
breaks = seq_along(unique(presidential$name)),
labels = unique(presidential$name)
) +
scale_x_date()
Using the geom_tile()
option you're forced to reparametrise the intervals, which can be a real pain for some classed objects like dates.
ggplot(presidential) +
geom_tile(
aes(x = structure((unclass(start) + unclass(end)) / 2, class = "Date"),
y = name,
width = end - start),
height = 0.8
) +
scale_y_discrete(
limits = unique(presidential$name)
)
#> Don't know how to automatically pick scale for object of type <difftime>.
#> Defaulting to continuous.
Created on 2024-04-24 with reprex v2.1.0
In the above cases, the ideal parameterisation is xmin = start, xmax = end, y = name, height = 0.8
, but this currently isn't possible. Besides this, sometimes you might also wish to use mixed parameters and use, for example, ymin
and height
together.