Skip to content

Instantly share code, notes, and snippets.

@karawoo
Created November 13, 2017 23:13
Show Gist options
  • Save karawoo/14f4f46da900b09997d26171d092fb92 to your computer and use it in GitHub Desktop.
Save karawoo/14f4f46da900b09997d26171d092fb92 to your computer and use it in GitHub Desktop.
## Start with the code and data here: https://github.com/fivethirtyeight/data/blob/master/police-deaths/plot.R
## Then:
## Calculate total deaths by year
dat <- data_for_plot %>%
group_by(year) %>%
mutate(total = sum(count))
## Colors I chose
mycolors <- c("#00bf7b", "#59004d", "#ffcb89", "#a76e61", "#ac270d", "#7890ff",
"#6ca013", "#c2e05e", "#00300d", "#ff7b90")
## Plot as small multiples
p_small_mult <- ggplot(dat, aes(x = year)) +
## Gray background showing total
geom_area(aes(y = total), fill = "grey80", alpha = 0.7) +
## Individual areas for each category
geom_area(aes(y = count, fill = cat)) +
facet_wrap(~ cat, nrow = 2) +
## Further customization
scale_fill_manual(values = mycolors) +
scale_y_continuous(limits = c(0, max(dat$total) + 5), expand = c(0, 0)) +
theme_minimal() +
theme(
legend.position = "none",
axis.text = element_text(size = 7)
) +
labs(
y = "Total deaths",
x = "Year",
title = "On-duty police officer deaths",
subtitle = "Data: https://github.com/fivethirtyeight/data/police-deaths"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment