Skip to content

Instantly share code, notes, and snippets.

@jebyrnes
Last active March 11, 2020 21:02
Show Gist options
  • Save jebyrnes/2271a9898bfb39992c98e02ab377672e to your computer and use it in GitHub Desktop.
Save jebyrnes/2271a9898bfb39992c98e02ab377672e to your computer and use it in GitHub Desktop.
library(coronavirus)
library(dplyr)
library(ggplot2)
library(rnaturalearth)
library(sf)
library(ggrepel)
library(wesanderson)
coronavirus <- update_coronavirus()
usa <- ne_states(country = "United States of America", returnclass = "sf")
#make some spatial objects
usa_confirmed_sf <- coronavirus %>%
filter(Country.Region=="US") %>%
filter(type=="confirmed") %>%
st_as_sf(coords = c(x = "Long", y = "Lat"),
crs = st_crs(usa))
usa_points <- st_join(usa_confirmed_sf, usa) %>%
group_by(name, date) %>%
summarize(cases = sum(cases))
usa_polygons <- st_join(usa, usa_confirmed_sf) %>%
group_by(name, date) %>%
summarize(cases = sum(cases))
usa_states <- usa_polygons %>%
group_by(name, date) %>%
summarize(cases = sum(cases)) %>%
ungroup() %>%
group_by(name) %>%
arrange(date) %>%
mutate(total_cases = cumsum(cases)) %>%
ungroup() %>%
arrange(date, desc(cases)) %>%
mutate(name_fct = forcats::fct_rev(forcats::fct_inorder(name)))
usa_states <- usa_states[!is.na(usa_states$cases),] #complete is weird with sf objs
#who are the top 5 for easy labels
top5 <- usa_states %>%
group_by(name_fct, name) %>%
summarize(total_cases = sum(cases),
date = max(date)) %>%
ungroup() %>%
arrange(desc(total_cases)) %>%
slice(1L:5L)
#heatmap? weird date gaps
ggplot(usa_states,
aes(x = date, y = name_fct, fill = total_cases)) +
geom_tile(color = "white") +
scale_fill_viridis_c() +
theme_minimal()
#line chart better idea
set.seed(2019)
ggplot(usa_states,
aes(x = date, color = name_fct, y = total_cases)) +
geom_line() +
scale_color_manual(values = wes_palette("Darjeeling1", n = 37, type = "continuous")) +
theme_minimal(base_size=10) +
guides(color = "none") +
geom_text_repel(data = top5,
mapping = aes(label = name),
direction = "x",
force = 105) +
ylab("Total Confirmed Cases") +
xlab("") +
ggtitle("US Progression of Covid-19",
subtitle = paste0("Datat from ", format(Sys.time(), "%a %b %d %Y"), " via RamiKrispin/coronavirus for R")) +
theme(plot.title.position = "plot")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment