Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ordering for lines in geom_lineribbon() #172

Open
mccarthy-m-g opened this issue Mar 9, 2023 · 0 comments
Open

Support ordering for lines in geom_lineribbon() #172

mccarthy-m-g opened this issue Mar 9, 2023 · 0 comments

Comments

@mccarthy-m-g
Copy link

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:

  1. Group "a" ribbons
  2. Group "a" line
  3. Group "b" ribbons
  4. 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant