Skip to content

Instantly share code, notes, and snippets.

@mpettis
Created July 20, 2021 19:06
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 mpettis/ae839cbe86dd0a91ab8d46e71426f312 to your computer and use it in GitHub Desktop.
Save mpettis/ae839cbe86dd0a91ab8d46e71426f312 to your computer and use it in GitHub Desktop.
# Plot date ranges with color scheme, using scale_fill_manual()
library(tidyverse)
plot_df <-
tribble(
~start_date, ~end_date, ~cat,
"2021-01-01","2021-01-02","a",
"2021-01-03","2021-01-05","b"
) %>%
mutate(cat = factor(cat)) %>%
mutate(start_date = ymd(start_date)) %>%
mutate(end_date = ymd(end_date)) %>%
mutate(y=0) %>%
mutate(w = as.numeric(end_date - start_date))
plot_df
colr_map <- c("a"="red", "b"="blue")
ggplot(plot_df, aes(xmin=start_date, xmax=end_date + 1, ymin=y, ymax=y+1)) +
geom_rect(aes(fill=cat)) +
scale_fill_manual(values=colr_map)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment