0

I am currently trying to shade the region between two geom_lines in a ggplot of time-series data in R. I am trying to do this based on which line has the highest value at a given time (i.e if the blue line has the highest value shade the region between the two lines blue and vice versa). I have tried an approach taking the difference between my two variables and using zero as a reference point while using geom_ribbon but to no avail. This is the code I am currently using for my plot:

plot_against_xG = data_select %>%
  ggplot(aes(x = Date, GF = GF_rolling_5_game_avg, xG = xG_rolling_5_game_avg)) +
  geom_line(aes(y = GF_rolling_5_game_avg, colour="GF")) +
  geom_line(aes(y = xG_rolling_5_game_avg, colour="xG")) +
  ggtitle("Season 1") +
  ylab("Points") +
  scale_color_manual(values = c(GF = "blue", xG = "red"),
                     labels = c(GF = "GF", xG = "xG"))

plt11 <- ggplotly(plot_against_xG, tooltip = c("x", "GF", "xG"))

The resultant plot is:

basic-plot

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
  • 1
    There are several ways to do this, but perhaps the easiest is using [stat_difference](https://teunbrand.github.io/ggh4x/articles/Statistics.html#difference) from the ggh4x package – Allan Cameron Apr 24 '23 at 11:48

0 Answers0