Skip to content

Instantly share code, notes, and snippets.

@dhicks
Created September 15, 2021 22:02
Show Gist options
  • Save dhicks/191b68b4c991323a41c1a87f902cc531 to your computer and use it in GitHub Desktop.
Save dhicks/191b68b4c991323a41c1a87f902cc531 to your computer and use it in GitHub Desktop.
Waffle plots to illustrate the importance of base rates
library(tidyverse)
# Sys.unsetenv('GITHUB_PAT')
# remotes::install_github('https://github.com/hrbrmstr/waffle')
library(waffle)
## <https://www.npr.org/sections/goatsandsoda/2021/08/20/1029628471/highly-vaccinated-israel-is-seeing-a-dramatic-surge-in-new-covid-cases-heres-why>
## "half of Israel's seriously ill patients who are currently hospitalized were fully vaccinated at least five months ago"
# vaxx rate: 78%
# unvaxxed serious case rate: 178.7 per 100,000
# vaxxed serious case rate: 178.7/9
n = 500
vaxx_rate = .80
cases_uv = 200/1000
cases_vx = cases_uv / 5
vaxx_total = round(n*vaxx_rate)
vaxx_cases = round(n*vaxx_rate*cases_vx)
unvaxx_total = round(n*(1-vaxx_rate))
unvaxx_cases = round(n*(1-vaxx_rate)*cases_uv)
## Almost 50% of cases are in vaccinated people oh no!
vaxx_cases / (unvaxx_cases + vaxx_cases)
dataf = tribble(
~ vaxx, ~ disease, ~ n,
'vaccinated', 'healthy', vaxx_total - vaxx_cases,
'vaccinated', 'sick', vaxx_cases,
'unvaccinated', 'healthy', unvaxx_total - unvaxx_cases,
'unvaccinated', 'sick', unvaxx_cases
)
dataf |>
filter(disease == 'sick') |>
ggplot(aes(fill = vaxx, values = n)) +
geom_waffle(n_rows = 6) +
scale_fill_viridis_d(option = 'C',
name = '') +
coord_equal() +
theme_enhance_waffle() +
theme_void() +
theme(legend.position = 'bottom') +
labs(title = '44% of cases are in vaccinated people',
caption = 'Made up data to illustrate base rates')
ggsave('base_rate1.png',
height = 3, width = 5)
dataf |>
ggplot(aes(fill = disease, values = n)) +
geom_waffle() +
scale_fill_brewer(palette = 'Set1',
direction = -1,
name = '') +
facet_wrap(vars(vaxx), ncol = 1) +
coord_equal() +
theme_enhance_waffle() +
theme_void() +
theme(legend.position = 'bottom') +
labs(title = 'But vaccinated people are much less likely to get sick',
caption = 'Made up data to illustrate base rates')
ggsave('base_rate2.png',
height = 3, width = 5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment