Skip to content

Instantly share code, notes, and snippets.

@mikkelkrogsholm
Created November 7, 2020 10:24
Show Gist options
  • Save mikkelkrogsholm/d97fb8f0af0a8cc3a67073281e2170ee to your computer and use it in GitHub Desktop.
Save mikkelkrogsholm/d97fb8f0af0a8cc3a67073281e2170ee to your computer and use it in GitHub Desktop.
Code comparing covid cases and deaths in Denmark
# Load the tidyverse
library(tidyverse)
# Load the data from Our World in Data
csv_url <- "https://covid.ourworldindata.org/data/owid-covid-data.csv"
data <- read_csv(csv_url)
# Filter the data to only get Denmark and data after feb 27
pd <- data %>%
filter(location == "Denmark") %>%
select(date, new_cases_smoothed_per_million, new_deaths_smoothed_per_million) %>%
filter(date >= "2020-02-27")
# Create a table with dates of the recent restrictions
restrictions <- tribble(~date, ~y, ~text,
as.Date("2020-07-07"), 2.5, "Forsamling: 100",
as.Date("2020-08-14"), 5, "Lukketid udvides",
as.Date("2020-08-22"), 7.5, "Masker i offentlig transport",
as.Date("2020-09-09"), 10, "Restriktioner natteliv København/Odense",
as.Date("2020-09-18"), 12.5, "Restriktioner restauranter mv.",
as.Date("2020-09-26"), 15, "Restriktioner private fester",
as.Date("2020-10-29"), 17.5, "Masker alle off. steder, forsamling 10 mv.") %>%
inner_join(pd)
# Create helper variables to help with the plotting
n = 10
ymax = max(pd$new_cases_smoothed_per_million, na.rm = T) / n
ymax = ceiling(ymax / 5) * 5
ymin = max(pd$new_deaths_smoothed_per_million, na.rm = T)
ymin = (ceiling(ymin / 1) * 1) * -1
mindate <- min(pd$date)
# Build the plot
ggplot(pd) +
geom_ribbon(aes(date, ymin = 0, ymax = new_cases_smoothed_per_million / n), fill = "darkgreen") +
geom_ribbon(aes(date, ymin = 0, ymax = -new_deaths_smoothed_per_million), fill = "red") +
theme_minimal() +
scale_y_continuous(breaks = c(seq(ymin, 0, 1), seq(0, ymax, 5)),
labels = c(seq(ymin, 0, 1) * -1, seq(0, ymax, 5)*n),
limits = c(ymin, ymax)) +
geom_text(data = restrictions, aes(x = date - 2, y = y, label = text), hjust = 1, color = "#808080") +
geom_segment(data = restrictions, aes(x = date, xend = date, y = y, yend = (new_cases_smoothed_per_million / n) + .5),
arrow = arrow(length = unit(0.02, "npc")),
color = "#808080") +
geom_segment(data = restrictions, aes(x = date, xend = date,
y = -new_deaths_smoothed_per_million,
yend = (new_cases_smoothed_per_million / n)),
color = "white", linetype = "dotted") +
geom_point(data = restrictions, aes(x = date, y = y), color = "#808080") +
annotate(geom = "text", x = mindate, y = 3.5, label = "Nye syge", hjust = 0, color = "darkgreen") +
annotate(geom = "text", x = mindate, y = -2, label = "Nye døde", hjust = 0, color = "red") +
scale_x_date(date_breaks = "1 month", date_labels = "%d/%m") +
theme(panel.grid.minor = element_blank()) +
labs(y = "Antal per million (obs: forskel i trin)", x = "",
caption = "Lavet af Mikkel Freltoft Krogsholm, Data fra Our World in Data.")
# Save the plot
ggsave(filename = "comparisons.png", width = 15, height = 7.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment