Last active
March 24, 2022 19:32
-
-
Save mschnetzer/4aaebeee2716e01d015ae71f20d150b8 to your computer and use it in GitHub Desktop.
Climate spiral for Austria (https://twitter.com/matschnetzer/status/1507062559106637831?s=20&t=_EiODPN1J8Mlv5ZEh0f37g)
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(lubridate) | |
library(msthemes) | |
library(gganimate) | |
# Get data from https://zamg.ac.at/histalp/dataset/station/csv.php | |
raw <- read_csv2("HISTALP_AT_WIE_T01_1760_2025.csv", skip = 13) | |
temp <- raw %>% select(year:dec) %>% | |
pivot_longer(names_to = "month", values_to = "value", cols = jan:dec) %>% | |
filter(year %in% 1900:2021) | |
findf <- temp %>% | |
with_groups(month, mutate, abw = value*0.1 - mean(value*0.1)) %>% | |
with_groups(year, mutate, abw_yr = mean(value*0.1) - mean(temp$value*0.1)) %>% | |
mutate(date = dmy(paste("01",month,year)), | |
reveal_time = rep(1:(n()/4), each = 4)) | |
output <- findf %>% | |
ggplot(aes(x = month(date, label=T), y = abw, color = abw_yr, group = year)) + | |
geom_line(size = 0.6) + | |
geom_hline(yintercept = 0, color = "white") + | |
geom_hline(yintercept = c(-4,4), color = c("skyblue3","red1"), size = 0.2) + | |
geom_vline(xintercept = 1:12, color = "white", size = 0.2) + | |
annotate("label", x = 12.5, y = seq(-4,4,4), label = c("-4°C","0°C","+4°C"), | |
color = c("skyblue3","white","red1"), size = 2.5, fill = "#464950", | |
label.size = NA, label.padding = unit(0.1, "lines"),) + | |
geom_point(x = 1, y = -11, size = 15, color = "#464950") + | |
geom_label(aes(x = 1, y = -11, label = year), | |
color = "white", size = 4, | |
fill = "#464950", label.size = NA) + | |
coord_polar(start = 0) + | |
scale_y_continuous(breaks = seq(-4,4,4)) + | |
scale_color_gradientn(colors = rev(RColorBrewer::brewer.pal(n=11, name = "RdBu")), | |
limits = range(findf$abw_yr)) + | |
labs(x = "", y = "", | |
title = "Klima-Spirale in Österreich", | |
subtitle = "Abweichung der Temperatur vom Durchschnitt 1900-2021 in Wien", | |
caption = "Idee: Ed Hawkins. Grafik: @matschnetzer") + | |
theme_ms(dark = T, grid = F, alttf = T) + | |
theme(axis.text.x = element_text(margin = margin(t = -20, unit = "pt")), | |
axis.text.y = element_blank(), | |
plot.subtitle = element_text(size = 9), | |
legend.position = "none") | |
anim <- output + | |
transition_manual(reveal_time, cumulative = T) + | |
ease_aes('linear') | |
gifout <- animate(anim, end_pause = 30, nframes = 366, | |
height = 5, width = 5, units = "in", res = 300) | |
anim_save("climatespiral.gif", gifout) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment