Skip to content

Instantly share code, notes, and snippets.

@mschnetzer
Last active April 2, 2020 14:09
Show Gist options
  • Save mschnetzer/d4f22cac7829acb3a503c6820f2aa473 to your computer and use it in GitHub Desktop.
Save mschnetzer/d4f22cac7829acb3a503c6820f2aa473 to your computer and use it in GitHub Desktop.
Animated development of unemployment in Austria (https://twitter.com/matschnetzer/status/1245709727356641280)
library(tidyverse)
library(lubridate)
library(msthemes)
library(gganimate)
unemp <- read_csv("unemployment.csv")
df <- unemp %>% filter(period == "month", indicator == "Unemployment registered, total, M-end NSA") %>%
select(year,month,values) %>%
mutate(values = values*1000) %>%
mutate(date = ymd(paste(year, month, "1", sep="-"))) %>%
group_by(month) %>% arrange(year) %>%
mutate(change = values - lag(values)) %>%
mutate(col = ifelse(change > 0, "growth", "decline")) %>%
ungroup() %>%
mutate(begin = 1:n(), length=n()-begin+200, grow = 1) %>%
mutate(begin = ifelse(change < 150000, begin, begin+100),
length = ifelse(change < 150000, length, length-100),
grow = ifelse(change < 150000, grow, grow+100))
anim <- df %>% filter(year > 1960) %>%
ggplot(aes(x=date, y=change, fill=col, color=NULL)) +
geom_bar(stat = "identity", aes(group=seq_along(date))) +
scale_fill_manual(values = msc_palette[c(4,1)]) +
scale_y_continuous(labels=function(x) format(x, big.mark = ",")) +
theme_ms(alttf = T) +
theme(legend.position = "none") +
labs(x="",y="",
title = "Arbeitslosigkeit in Österreich",
subtitle = "Veränderungen der Arbeitslosenzahl zum Vorjahresmonat",
caption = "Daten: OeNB/AMS. Grafik: @matschnetzer") +
transition_events(start = as.integer(begin),
end = as.integer(begin + length),
enter_length = as.integer(grow),
exit_length = as.integer(1)) +
enter_grow() +
view_follow() +
ease_aes('linear')
animate(anim, width = 8, height = 4.5, res = 150, unit = "in", end_pause = 40)
anim_save("unemployment.gif")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment