Skip to content

Fix for geom_ribbon() #3432

Closed
Closed
@robwschlegel

Description

@robwschlegel

Hello,
Firstly, thank you for providing the world with the ggplot2 package and broader tidyverse ecosystem of packages. It has revolutionised the way people use R and it is the only method I use now when introducing my new students to the language.
I work in climate science and so have a series of requirements for visuals that are perhaps a bit different than data science more broadly. One of these is the necessity to be able to visualise values that are in excess of a given threshold. One would normally use the geom_ribbon() function to accomplish this, but it is my understanding that this function does not have the capability to allow users to choose to fill in the area only above or below one of the given lines (i.e. ymin or ymax). There are multiple posts on stack overflow etc. about this and the work arounds provided do not create very nice figures. Specifically I mean that the corners of the polygons are not satisfactorily filled in. For this reason I went about creating a fix for this issue. The geom I created is called geom_flame() and may be found in my package heatwaveR, which is on CRAN. I have also written the necessary code to allow this geom to work in an interactive plotly environment.
If it would be deemed appropriate I would be happy for my source code to be included in ggplot2 in part or in full, as necessary. Below please find a reprex highlighting the differences between geom_flame() and geom_ribbon(). I did not include a reprex for use in plotly as I don't think that works in GitHub(?).
All the best,
-Robert Schlegel

# Load libraries
library(ggplot2)
library(heatwaveR)

# Detect the marine heatwaves in a time series
ts <- ts2clm(sst_WA, climatologyPeriod = c("1982-01-01", "2011-12-31"))
mhw <- detect_event(ts)

# Grab a subset of data
data_stub <- mhw$climatology[10580:10720,]

# Visualise with geom_ribbon
ribbon_plot <- ggplot(data_stub, aes(x = t, ymin = temp, ymax = thresh)) +
  geom_ribbon(fill = "salmon") +
  geom_line(aes(y = temp)) +
  geom_line(aes(y = thresh), colour = "darkgreen")

# Visualise with geom_flame
flame_plot <- ggplot(data_stub, aes(x = t, y = temp, y2 = thresh)) +
  geom_flame() +
  geom_line() +
  geom_line(aes(y = thresh), colour = "darkgreen")

# Stick them next to each other
cowplot::plot_grid(ribbon_plot, flame_plot, ncol = 1)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions