Last active
February 3, 2020 20:18
-
-
Save matteodefelice/65c3922aa485bb33f6b86dfcd6a88b59 to your computer and use it in GitHub Desktop.
R script to produce the plot shown here: https://twitter.com/matteodefelice/status/1224426544803827713
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(eurostat) | |
library(lubridate) | |
n_veih <- get_eurostat("tran_r_vehst") %>% # https://appsso.eurostat.ec.europa.eu/nui/show.do?dataset=tran_r_vehst&lang=en | |
dplyr::filter( | |
str_length(geo) == 2, | |
unit == "P_THAB", # NR: Number, P_THAB: Per thousand inhabitants | |
vehicle == "CAR" # Passenger cars | |
) | |
accid <- get_eurostat("tran_r_acci") %>% # https://appsso.eurostat.ec.europa.eu/nui/show.do?dataset=tran_r_acci&lang=en | |
dplyr::filter( | |
str_length(geo) == 2, | |
unit == "P_MHAB", # NR: Number, P_THAB: Per million inhabitants | |
victim == "KIL" # KIL: killed, INJ: injured | |
) | |
pop <- get_eurostat("tgs00096") # https://ec.europa.eu/eurostat/databrowser/view/tgs00096/default/table?lang=en | |
### | |
toplot <- inner_join( | |
n_veih %>% select(geo, time, vehicles = values), | |
accid %>% select(geo, time, kills = values), | |
by = c("geo", "time") | |
) %>% | |
dplyr::filter( | |
geo %in% c("ES", "IT", "DE", "FR", "UK", "BE") | |
) %>% | |
group_by(geo) %>% | |
arrange(time) %>% | |
mutate( | |
kills_smooth = zoo::rollmean(kills, k = 5, align = "right", fill = NA) | |
) %>% | |
dplyr::filter( | |
!is.na(kills_smooth) | |
) %>% | |
ungroup() %>% | |
group_by(geo) %>% | |
arrange(time) %>% | |
ungroup() | |
g <- ggplot(toplot, aes(x = vehicles, y = kills_smooth, color = geo, group = geo)) + | |
geom_point(aes(size = year(time)), alpha = 0.2) + | |
geom_point( | |
data = toplot %>% | |
dplyr::filter(year(time) == 2017), | |
aes(size = year(time)), alpha = 0.7 | |
) + | |
geom_path() + | |
theme_light() + | |
scale_color_brewer(palette = "Set2", name = "country") + | |
scale_size_continuous(breaks = c(1994, 2017), name = "year") + | |
labs( | |
x = "Number of cars (per 1000 inhabitants)", | |
y = "Kills due to car accidents (per million inhabitants)", | |
title = "Cars in Europe: density and death rates", | |
subtitle = "Time evolution for six European countries (from 1994 to 2017)", | |
caption = "Source EUROSTAT data (datasets `tran_r_acci` and `tran_r_vehst`)" | |
) + | |
theme( | |
text = element_text(size = 12), | |
plot.title = element_text(size = 16, face = "bold") | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment