1

I'm trying to build a plot with two lines and fill the area between with geom_ribbon. I've managed to select a fill color (red/blue) depending on the sign of the difference between two lines. First I create two new columns in the dataset for ymax, ymin. It seems to work but some spurious lines appear joining red areas.

Is geom_ribbon appropriate to fill the areas? Is there any problem in the plot code?

This is the code used to create the plot

datos.2022 <- datos.2022 %>% mutate(y1 = SSTm-273.15, y2 = SST.mean.day-273.15)

datos.2022 %>% ggplot(aes(x=fecha)) + 
  geom_line(aes(y=SSTm-273.15), color = "red") +
  geom_line(aes(y=SST.mean.day - 273.15), color = "black") +
  geom_ribbon(aes(ymax=y1, ymin = y2, fill = as.factor(sign)), alpha = 0.6) +
  scale_fill_manual(guide = "none", values=c("blue","red")) +
  scale_y_continuous(limits = c(10,30)) +
  scale_x_date(expand = c(0,0), breaks = "1 month", date_labels = "%b" ) + 
  theme_hc() + 
  labs(x="",y ="SST",title = "Temperature (2022)") + 
  theme(text = element_text(size=20,family = "Arial"))

And this is the output

enter image description here

Example data for the plot available at https://www.dropbox.com/s/mkk8w7py2ynuy1t/temperature.dat?dl=0

pacomet
  • 5,011
  • 12
  • 59
  • 111
  • 2
    This happens because even the separate red bits are considered one group that should be connected. There is the [{ggbraid}](https://nsgrantham.github.io/ggbraid/) package or [`ggh4x::stat_difference()`](https://teunbrand.github.io/ggh4x/articles/Statistics.html#difference) that work specifically for such cases. (Disclaimer: I wrote ggh4x) – teunbrand Feb 01 '23 at 12:40
  • Fantastic package, just a problem. ggbraid is not available for my R version R version 4.2.2 Patched (2022-11-10 r83330) running under: Ubuntu 20.04.5 LTS. Could not install in my RStudio. – pacomet Feb 01 '23 at 12:53
  • 1
    I think ggbraid might be underway to CRAN, but you can install from github as well `remotes::install_github("nsgrantham/ggbraid")`. – teunbrand Feb 01 '23 at 12:54

1 Answers1

2

What if you made two different series to plot as ribbons - one for the positive values where there is no distance between ymin and ymax for the places where the difference is negative. And one for the negative values that works in a similar way.

library(dplyr)
library(ggplot2)

datos.2022 <- datos.2022 %>% 
  mutate(y1 = SSTm-273.15, 
         y2 = SST.mean.day-273.15) %>% 
  rowwise() %>% 
  mutate(high_pos = max(SST.mean.day - 273.15, y1), 
         low_neg = min(SSTm-273.15, y2))


datos.2022 %>% ggplot(aes(x=fecha)) + 
  geom_line(aes(y=SSTm-273.15), color = "red") +
  geom_line(aes(y=SST.mean.day - 273.15), color = "black") +
  geom_ribbon(aes(ymax=high_pos, ymin = SST.mean.day - 273.15, fill = "b"), alpha = 0.6, col="transparent", show.legend = FALSE) +
  geom_ribbon(aes(ymax = SST.mean.day - 273.15, ymin = low_neg, fill = "a"), alpha = 0.6, col="transparent", show.legend = FALSE) +
  scale_fill_manual(guide = "none", values=c("blue","red")) +
  scale_y_continuous(limits = c(10,30)) +
  scale_x_date(expand = c(0,0), breaks = "1 month", date_labels = "%b" ) + 
  #theme_hc() + 
  labs(x="",y ="SST",title = "Temperature (2022)") + 
  theme(text = element_text(size=20,family = "Arial"))

DaveArmstrong
  • 18,377
  • 2
  • 13
  • 25
  • That's a nice workaround that works exactly the way I want. Now the plot is OK but it does not look "nice" to use two different ribbons. Thanks. – pacomet Feb 01 '23 at 12:48