-
Notifications
You must be signed in to change notification settings - Fork 17
/
22-animation-temperature.R
147 lines (129 loc) · 5.54 KB
/
22-animation-temperature.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
library(tidyverse)
library(lubridate)
library(here)
library(ggtext)
library(gganimate)
#' https://commons.wikimedia.org/wiki/File:20171231_Climate_spiral_(HadCRUT4.6_1850-_)_Ed_Hawkins.gif
base_path <- here("2022", "22")
#' Download historical weather data (DWD)
data_urls <- paste0(
"https://opendata.dwd.de/climate_environment/CDC/regional_averages_DE/monthly/air_temperature_mean/regional_averages_tm_",
str_pad(1:12, 2, "left", "0"),
".txt")
df_raw <- read_delim(data_urls, delim = ";", trim_ws = TRUE,
locale = locale(decimal_mark = "."), skip = 1, id = NULL)
df_long <- df_raw %>%
mutate(Monat = as.numeric(str_remove(Monat, "^0"))) %>%
select(-`...20`) %>%
rename(year = 1, month = Monat) %>%
pivot_longer(cols = -c(year, month), names_to = "territory", values_to = "avg_temp") %>%
filter(territory == "Deutschland") %>%
# exclude 2022
filter(year < 2022) %>%
# calculate avg. temperature for each year
group_by(year) %>%
mutate(annual_avg_temp = mean(cur_data()$avg_temp)) %>%
ungroup() %>%
arrange(year, month) %>%
mutate(month_name = fct_inorder(month.name[month]))
first_year <- min(df_long$year)
last_year <- max(df_long$year)
# Baseline temperature
baseline_temp <- df_long %>%
filter(year >= 1881, year <= 1910) %>%
summarize(baseline_temp = mean(avg_temp)) %>%
pull(baseline_temp)
p <- df_long %>%
ggplot(aes(month_name, avg_temp, group = year, col = annual_avg_temp)) +
geom_line(size = 0.6, alpha = 1) +
# geom_text(data = . %>% filter(month == 1),
# aes(label = year),
# hjust = 1, family = "Fira Sans", fontface = "bold",
# nudge_y = 2) +
geom_text(data = . %>% filter(month == 1),
aes(x = 0.5, y = 2, label = year),
hjust = 0.5, family = "Fira Sans", fontface = "bold",
nudge_y = 2) +
scale_color_gradient2(low = "blue", mid = "grey95", high = "red", midpoint = baseline_temp) +
coord_polar() +
guides(col = guide_colorbar(title.position = "top")) +
labs(
title = glue::glue("Average monthly temperature in Germany {first_year} to {last_year}"),
caption = "Baseline: 1881-1910 **Source:** DWD CDC | **Visualization:** Ansgar Wolsing",
y = "Avg. monthly temperature (°C)",
col = "Avg. annual temperature (°C)"
) +
theme_minimal(base_family = "Fira Sans") +
theme(
plot.background = element_rect(color = NA, fill = "grey1"),
panel.grid = element_blank(),
panel.grid.major.y = element_line(color = "grey30", size = 0.2),
text = element_text(color = "grey89"),
plot.subtitle = element_text(size = 20),
legend.text = element_text(color = "grey87"),
axis.title.x = element_blank(),
axis.title.y = element_text(hjust = 0.7, color = "grey60"),
axis.text = element_text(color = "grey40"),
plot.title = element_markdown(color = "grey99", hjust = 0.5),
plot.title.position = "plot",
plot.caption = element_markdown(hjust = 0.5),
plot.caption.position = "plot",
legend.position = "bottom",
legend.key.height = unit(2, "mm"),
legend.key.width = unit(12, "mm"),
legend.title.align = 0.5,
axis.ticks.y = element_line(size = 0.1),
)
## Version with deviations from baseline instead of absolute temperatures ------
# Baseline temperature (1881-1910)
baseline_temp_month <- df_long %>%
filter(year >= 1881, year <= 1910) %>%
group_by(month) %>%
summarize(baseline_temp = mean(avg_temp)) %>%
pull(baseline_temp)
p2 <- df_long %>%
mutate(avg_temp_deviation = avg_temp - baseline_temp_month[month]) %>%
ggplot(aes(month_name, avg_temp_deviation, group = year, col = annual_avg_temp)) +
# geom_hline(yintercept = c(0, 1, 2), color = "green") +
geom_line(size = 0.6, alpha = 1) +
geom_text(data = . %>% filter(month == 1),
aes(x = 0.5, y = 2, label = year),
hjust = 0.5, family = "Fira Sans", fontface = "bold",
nudge_y = 2) +
scale_color_gradient2(low = "blue", mid = "grey95", high = "red", midpoint = baseline_temp) +
coord_polar() +
guides(col = guide_colorbar(title.position = "top")) +
labs(
title = glue::glue("Average monthly temperature in Germany {first_year} to {last_year}"),
caption = "Baseline: 1951-1980. **Source:** DWD CDC | **Visualization:** Ansgar Wolsing",
y = "Avg. monthly temperature (°C)",
col = "Avg. annual temperature (°C)"
) +
theme_minimal(base_family = "Fira Sans") +
theme(
plot.background = element_rect(color = NA, fill = "grey1"),
panel.grid = element_blank(),
panel.grid.major.y = element_line(color = "grey30", size = 0.2),
text = element_text(color = "grey89"),
plot.subtitle = element_text(size = 20),
legend.text = element_text(color = "grey87"),
axis.title.x = element_blank(),
axis.title.y = element_text(hjust = 0.7, color = "grey60"),
axis.text = element_text(color = "grey40"),
plot.title = element_markdown(color = "grey99", hjust = 0.5),
plot.title.position = "plot",
plot.caption = element_markdown(hjust = 0.5),
plot.caption.position = "plot",
legend.position = "bottom",
legend.key.height = unit(2, "mm"),
legend.key.width = unit(12, "mm"),
legend.title.align = 0.5,
axis.ticks.y = element_line(size = 0.1),
)
## Animation -------------------------------------------------------------------
p_anim <- p +
transition_time(year) +
shadow_mark(size = size / 5, alpha = 0.75 * alpha, exclude_layer = 2) # exclude geom_text
animate(p_anim, res = 200, width = 1000, height = 1200,
fps = 6, end_pause = 20, bg = "grey1")
anim_save(here(base_path, "22-temperature-de.gif"))