-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathday12_strips.Rmd
207 lines (160 loc) · 7.57 KB
/
day12_strips.Rmd
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
---
title: "Day 12: distribution - strips"
author: "Christophe Nicault"
date: "14/04/2021"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r load_packages}
library(tidyverse)
library(lubridate)
library(patchwork)
library(ragg)
library(showtext)
library(ggfx)
font_add_google("Roboto", "roboto")
font_add_google("Roboto Condensed", "roboto condensed")
font_add_google("Oswald", "oswald")
font_add_google("Share Tech Mono", "techmono")
showtext_opts(dpi = 320)
showtext_auto(enable = TRUE)
```
The data can be found there :
https://www.kaggle.com/berkeleyearth/climate-change-earth-surface-temperature-data?select=GlobalLandTemperaturesByCountry.csv
```{r load_data}
climate <- vroom::vroom(here::here("input", "GlobalLandTemperaturesByCountry.csv"))
```
```{r colors}
txt_clr <- "white"
pal1 <- c("#105182", "#1a7bc5", "#42a2f1", "#E9F1F2", "#ff9193", "#f1434a", "#c91022", "#8d0613", "#4D030A")
```
```{r monthly_average}
monthly <- climate %>%
filter(Country == "France", !is.na(AverageTemperature)) %>%
mutate(year = lubridate::year(dt),
month = lubridate::month(dt, label = TRUE),
pos = lubridate::month(dt, label = FALSE),
color = ifelse(year > 1980, "Recent", "Past")) %>%
filter(year >=1900)
axis_labels <- tibble(month = lubridate::month(seq(1,12,1), label = TRUE),
pos = seq(1,12,1))
seg <- tibble(x = c(0, 0, 10, 0, 9, 3, 8, 5, 6),
xend = c(12.5, 3, 12.5, 5, 12.5, 6, 11, 10, 8),
y = c(0, 5, 5, 10, 10, 15, 15, 20, 25),
yend = c(0, 5, 5, 10, 10, 15, 15, 20, 25))
seg_lab <- tibble(x = c(0, 0, 0, 3, 5, 6),
y = seq(0,25, 5))
monthly_plt <- ggplot() +
# empty tile to get a legend with rectangle key
geom_tile(data = monthly, aes(x = 0, y =0, width =0, height = 0, fill = color)) +
# y-axis
geom_segment(data = seg, aes(x = x, xend = xend, y = y, yend = yend), color = "white", linetype = "12") +
geom_text(data = seg_lab, aes(x = x, y = y, label = glue::glue("{y} °C")), color = "white", nudge_y = 1, family = "roboto condensed", hjust = 0) +
# show.legend = FALSE to remove the shape of the point in the legend
geom_jitter(data = filter(monthly, color == "Recent"), aes(x = pos+0.2, y = AverageTemperature, fill = color), width = 0.15, height =0, size = 3, shape = 21, stroke = 0.3, color = "#FFDADC", show.legend = FALSE) +
geom_jitter(data = filter(monthly, color == "Past"), aes(x = pos-0.2, y = AverageTemperature, fill = color), width = 0.15, height =0, size = 2.5, shape = 21, stroke = 0.3, color = "#93E2F5", show.legend = FALSE) +
# x-axis labels
geom_text(data = axis_labels, aes(x = pos, y = -2, label = month), color = "white", vjust = 0, angle = 90, size = 5, family = "oswald")+
# scales
scale_fill_manual(values = c("Recent" = "#f1434a", "Past" = "#1a7bc5"), labels = c("Recent" = "> 1980", "Past" = "<= 1980")) +
scale_y_continuous(limits = c(-4,26), breaks = seq(0,25,5)) +
labs(fill = "Observations") +
theme_void() +
guides(fill = guide_legend(label.position = "top",
title.hjust = 0.5,
keyheight = unit(1, "line"),
keywidth = unit(4, "line"),
nrow = 1),
color = FALSE) +
theme(plot.background = element_rect(fill = "grey40", color = NA),
legend.position = c(0.13, 0.85),
legend.text = element_text(face = "bold", size = 12, color = txt_clr),
legend.title = element_text(face = "bold", size = 14, color = txt_clr))
```
Function to change the color from continuous to discrete
```{r color_function}
map_col <- function(x){
col_range[which(x > col_range$range1 & x <= col_range$range2 ),]$col
}
```
```{r yearly_average}
cty_avg <- climate %>%
filter(Country == "France") %>%
mutate(year = lubridate::year(dt)) %>%
filter(year >=1900 & year < 2000) %>%
group_by(Country) %>%
summarise(country_avg = mean(AverageTemperature, na.rm = TRUE))
col_range <- tibble(range1 = seq(-2.1, 2.7, 0.6),
range2 = seq(-1.5, 3.3, 0.6),
col = paste0("col", seq(1, 9, 1)))
# remove palette first in case of testing another one, to avoid duplicate values
palette <- NULL
palette[paste0("col", seq(1, 9, 1))] <- pal1
climate_gap <- climate %>%
filter(Country %in% c("France")) %>%
mutate(year = lubridate::year(dt)) %>%
filter(year >=1900) %>%
group_by(Country, year) %>%
summarise(y_avg = mean(AverageTemperature, na.rm = TRUE)) %>%
ungroup() %>%
left_join(cty_avg) %>%
mutate(temp_gap = y_avg - country_avg) %>%
mutate(temp_disc = map_chr(temp_gap, map_col))
legendpos <- col_range %>%
filter(range1 > -2.1, range1 < 2.1)
legend_text <- tibble(text = unique(round(c(legendpos$range1, legendpos$range2),1))) %>%
mutate(pos = row_number() %% 2 / 2)
# Yearly plot
yearly_plt <- ggplot()+
as_reference(
geom_rect(data = climate_gap, aes(xmin = year, xmax = year+1, ymin = -2, ymax = 3, fill = temp_disc)) ,
id = "strip"
) +
scale_fill_manual(values = palette) +
with_blend(
# the offset of 0.5 is to account for the size of the line
geom_line(data = climate_gap, aes(x = year+0.5, y = temp_gap), size = 3),
bg_layer = "strip",
blend_type = "pegtop_light"
)+
scale_y_continuous(limits = c(-2.2, 3), expand = c(0, 0)) +
scale_x_continuous(breaks = seq(1900, 2020, 10))+
guides(fill = FALSE) +
labs(title = "Annual deviation from 20th century average") +
theme_void() +
theme(plot.background = element_rect(fill = "grey40", color = NA),
plot.title = element_text(family = "oswald", size = 18, color = txt_clr, hjust = 0.5, face = "bold", margin = margin(10,0,20,0)),
axis.text.x = element_text(size = 11, color = "white", family = "roboto"),
axis.ticks.x = element_line(color = "white", ),
axis.ticks.length.x = unit(2, "mm"),
plot.margin = margin(0, 0, 90, 0))
# legend
legend <- ggplot() +
geom_rect(data = legendpos, aes(xmin = range1, xmax = range2, ymin = 0, ymax = 1, fill = col)) +
geom_text(data = legend_text, aes(x = text, y = 1.6 + pos, label = glue::glue("{text} °C")), size = 3.5, color = "white", vjust = 0, family = "roboto")+
geom_segment(data = legend_text, aes(x = text, xend = text, y = 1, yend = 1.5 + pos), linetype = "13", color = "white") +
scale_fill_manual(values = palette) +
scale_y_continuous(limits = c(0, 2.5)) +
guides(fill = FALSE) +
theme_void() +
theme(plot.background = element_rect(fill = "grey40", color = NA))
```
```{r assemble}
final <- monthly_plt / (yearly_plt + inset_element(legend, 0.2, 0.1, 0.8, 0.27, align_to = "full")) + plot_layout(heights = c(5,1)) +
plot_annotation(
title = "Temperature evolution in France",
subtitle = "The strip chart shows the monthly average before / after 1980.\nThe stripes shows the yearly temperature deviation from 20th century average.",
caption = "#30DayChartChallenge Day 12 | Viz: Christophe Nicault | Data: Berkeley Earth / Kaggle",
theme = theme(
plot.background = element_rect(fill = "grey40", color = NA),
plot.title = element_text(family = "oswald", size = 20, color = txt_clr, hjust = 0.5, face = "bold", margin = margin(10,0,0,0)),
plot.subtitle = element_text(family = "oswald", color = txt_clr, size = 16, hjust = 0.5, margin = margin(10,0,15,0)),
plot.caption = element_text(family = "techmono", color = txt_clr, size = 11, hjust = 0.98)
)
)
ragg::agg_png(here::here("render", paste0("30DayChartChallenge_day12", format(Sys.time(), "%Y%m%d_%H%M%S"), ".png")), res = 320, width = 10, height = 10, units = "in")
final
dev.off()
```