Skip to content

Gradients in ribbons #5699

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

Merged
merged 7 commits into from
May 20, 2024
Merged

Gradients in ribbons #5699

merged 7 commits into from
May 20, 2024

Conversation

teunbrand
Copy link
Collaborator

This PR aims to fix #4690.

Briefly, geom_ribbon() (and by extension geom_area()) didn't allow varying fill or alpha aesthetics within a group.
This PR encodes fill and/or alpha in a grid::linearGradient(), which allows these aesthetics to vary.

Demo for fill:

devtools::load_all("~/packages/ggplot2/")
#> ℹ Loading ggplot2

ggplot(economics, aes(date, unemploy, fill = uempmed)) +
  geom_area(stat = "identity") +
  scale_fill_viridis_c()

Demo for alpha:

ggplot(economics) +
  aes(date, ymin = unemploy - 200, ymax = unemploy + 200, alpha = pop) +
  geom_ribbon()

Created on 2024-02-22 with reprex v2.1.0

A few limitations:

  • Using this in non-linear coordinates, say polar coordinates, is a bad idea as we cannot do gradient curves or conical gradients to morph the gradient appropriately. Therefore, varying fill or alpha in non-linear coordinates still gives the previous error.
  • The default stat = "align" in geom_area() does not interpolate the fill or alpha at break points, which is then given a single flat fill/alpha.
  • In theory, this will also form a gradient between discrete fills if user overrides the default group aesthetic. At the GeomRibbon$draw_group() method, it is no longer possible to discern whether fill was continuous or discrete, so there is no convenient way around this.

Copy link
Member

@thomasp85 thomasp85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@teunbrand teunbrand merged commit f8dac9d into tidyverse:main May 20, 2024
11 checks passed
@teunbrand teunbrand deleted the ribbon_gradients branch May 20, 2024 10:18
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

Successfully merging this pull request may close these issues.

geom_ribbon: aesthetics can not vary with a ribbon
2 participants