Support ordering for lines in `geom_lineribbon()`
mccarthy-m-g opened this issue · comments
This issue is continuing the conversation in #171. See #171 (comment) onwards.
Request
71f346b added support for an explicit order aesthetic for geom_lineribbon()
, making it possible to group ribbons together in layers as demonstrated in the reprex below. It would be nice if it was also possible to control the order of lines so they could be grouped with their ribbons. For example, in the plot below it would be nice to have a way to order the ribbons and lines in the following layers:
- Group "a" ribbons
- Group "a" line
- Group "b" ribbons
- Group "b" line
This isn't an urgent feature, and it's easy to workaround (see next section); but if it is possible it would be a nice to have.
library(tidyverse)
library(ggdist)
set.seed(1234)
n = 5000
df = tibble(
.draw = 1:n,
intercept = rnorm(n, 3, 1),
slope = rnorm(n, 1, 0.25),
x = list(-4:5),
y = map2(intercept, slope, ~ .x + .y * -4:5)
) %>%
unnest(c(x, y))
df_2groups = rbind(
mutate(df, g = "a"),
mutate(df, g = "b", y = (y - 2) * 0.5)
)
# Here it would be nice if there was a way to place the line for group "a"
# below the ribbons for group "b":
df_2groups %>%
ggplot(aes(x = x, y = y, fill = g)) +
stat_lineribbon(aes(
fill_ramp = after_stat(level),
order = after_stat(interaction(level, group))
)) +
labs(title = "stat_lineribbon(aes(order = after_stat(interaction(level, group))))") +
theme_ggdist()
Created on 2023-03-08 with reprex v2.0.2
Current workaround
A simple way to accomplish the desired result is to plot multiple geoms in the desired order using subsets of the data:
library(tidyverse)
library(ggdist)
set.seed(1234)
n = 5000
df = tibble(
.draw = 1:n,
intercept = rnorm(n, 3, 1),
slope = rnorm(n, 1, 0.25),
x = list(-4:5),
y = map2(intercept, slope, ~ .x + .y * -4:5)
) %>%
unnest(c(x, y))
df_2groups = rbind(
mutate(df, g = "a"),
mutate(df, g = "b", y = (y - 2) * 0.5)
)
ggplot(mapping = aes(x = x, y = y, fill = g, fill_ramp = after_stat(level))) +
stat_lineribbon(data = filter(df_2groups, g == "a")) +
stat_lineribbon(data = filter(df_2groups, g == "b")) +
theme_ggdist()
Created on 2023-03-08 with reprex v2.0.2