Skip to content

Instantly share code, notes, and snippets.

@andrewheiss
Created July 29, 2022 02:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewheiss/9d2a211a3846d259dfece6db1ffa15ff to your computer and use it in GitHub Desktop.
Save andrewheiss/9d2a211a3846d259dfece6db1ffa15ff to your computer and use it in GitHub Desktop.
library(tidyverse)

make_fancy_ribbon <- function(df, x, y, n = 1000) {
  interp <- approx(df[[x]], df[[y]], n = n)
  interp_df <- data.frame(interp$x, interp$y)
  colnames(interp_df) <- c(x, y)
  
  return(interp_df)
}

fake_data <- tribble(
  ~year, ~rep_margin,
  1800, -5,
  1804, -2,
  1808, 4,
  1812, 6,
  1816, -1,
  1820, 2,
  1824, 7
) 

plot_fake_data <- make_fancy_ribbon(fake_data, "year", "rep_margin") %>% 
  mutate(rep_win = rep_margin > 0)

ggplot(fake_data, aes(x = rep_margin, y = year)) +
  geom_point() +
  geom_area(data = plot_fake_data, aes(fill = rep_win), orientation = "y") +
  geom_vline(xintercept = 0) +
  scale_y_reverse()

Created on 2022-07-28 by the reprex package (v2.0.1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment