Skip to content

Instantly share code, notes, and snippets.

@tgerke
Last active July 10, 2020 15:09
Show Gist options
  • Select an option

  • Save tgerke/c801ab0efada7cca4efff9ccf5f62607 to your computer and use it in GitHub Desktop.

Select an option

Save tgerke/c801ab0efada7cca4efff9ccf5f62607 to your computer and use it in GitHub Desktop.
# need to do this within the repo at
# https://github.com/gadenbuie/covid19-florida
library(dplyr, warn.conflicts = FALSE)
library(tidyr, warn.conflicts = FALSE)
library(readr, warn.conflicts = FALSE)
library(lubridate, warn.conflicts = FALSE)
library(ggplot2, warn.conflicts = FALSE)
library(purrr, warn.conflicts = FALSE)
library(ggiraph, warn.conflicts = FALSE)
source(here::here("R/combine-sources.R"))
fix_late_reporting <- function(x, start, end, adjusted) {
return(x)
dplyr::mutate(
x,
timestamp = dplyr::if_else(
timestamp >= lubridate::ymd_hm(start, truncated = 2) &
timestamp <= lubridate::ymd_hm(end, truncated = 2),
lubridate::ymd_hm(adjusted, truncated = 2),
timestamp
)
)
}
line_list <- readr::read_csv("data/covid-19-florida_arcgis_line-list.csv", guess_max = 1e5)
dash <- readr::read_csv("data/covid-19-florida_arcgis_summary.csv", guess_max = 1e5) %>%
fix_late_reporting("2020-05-07 13", "2020-05-07 23", "2020-05-07 12:59")
# Test summary import -----------------------------------------------------
testing_summary_dash <- read_csv("data/covid-19-florida_dash_summary.csv") %>%
mutate_at("timestamp", lubridate::ymd_hms) %>%
fix_late_reporting("2020-05-07 13", "2020-05-07 23", "2020-05-07 12:59")
test_summary <-
combine_scraped_and_api_data(testing_summary_dash, dash)
g_test_changes <-
test_summary %>%
select(day, positive, deaths, pending) %>%
filter(!is.na(positive)) %>%
arrange(day) %>%
mutate(
positive = positive - lag(positive),
deaths = deaths - lag(deaths),
complete = if_else(day == today(), "today", "past")
) %>%
filter(!is.na(positive)) %>%
select(day, complete, positive, deaths) %>%
pivot_longer(cols = c(positive, deaths), names_to = "status") %>%
mutate_at(vars(status), tools::toTitleCase) %>%
mutate_at(vars(status), factor, levels = c("Positive", "Deaths")) %>%
mutate(weekday = wday(day, label = TRUE)) %>%
ggplot() +
aes(day, value) +
geom_col_interactive(aes(alpha = complete, fill = status, tooltip = weekday, data_id = weekday), width = 1) +
geom_line(
data = . %>%
group_by(status) %>%
mutate_at(vars(value), ~ slider::slide_dbl(.x, mean, .before = 7)),
aes(color = status),
size = 1
) +
labs(
x = NULL, y = NULL,
caption = glue::glue(
"Last update: {max(test_summary$timestamp)}",
"Source: Florida DOH",
"github.com/gadenbuie/covid19-florida",
.sep = "\n"
)
) +
ggtitle(
label = "Daily Change in Testing",
subtitle = "Florida COVID-19"
) +
scale_alpha_manual(
values = c(past = 1, today = 0.33),
guide = FALSE
) +
scale_color_manual(
values = c(Pending = "#63768d", Positive = "#ec4e20", Deaths = "#440154"),
guide = FALSE
) +
scale_fill_manual(
# values = c(Pending = "#63768d", Positive = "#ec4e20", Deaths = "#440154"),
values = c(Pending = "#63768d", Positive = "#f7b8a5", Deaths = "#b499ba"),
guide = FALSE
) +
theme_minimal(base_size = 14) +
scale_x_date(date_breaks = "1 month", expand = expand_scale(add = 0.5), date_labels = "%B") +
scale_y_continuous(expand = expansion()) +
coord_cartesian(clip = "off") +
theme(
plot.margin = margin(0.5, 0.5, 0.5, 0.5, unit = "lines"),
plot.subtitle = element_text(margin = margin(b = 1.25, unit = "lines")),
plot.caption = element_text(color = "#444444"),
panel.border = element_rect(color = "#dddddd", fill = NA),
strip.text = element_text(face = "bold"),
axis.title.y = element_text(angle = 0, hjust = 1),
# panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.minor.y = element_blank()
) +
facet_wrap(~ status, scales = "free_y")
girafe(ggobj = g_test_changes) %>%
girafe_options(
opts_tooltip(opacity = .7)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment