This repository has been archived by the owner on Nov 29, 2024. It is now read-only.
generated from 360-info/quarto-scaffold
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalysis.rmd
171 lines (148 loc) · 4.92 KB
/
analysis.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
---
title: "Nuclear weapons"
author: "James Goldie, 360info"
---
```{r setup}
library(tidyverse)
library(glue)
library(sf)
library(ggsflabel)
library(gganimate)
library(ggtext)
library(themes360info)
library(rnaturalearth)
library(here)
```
```{r import}
stockpiles <- read_csv(here("data", "nuclear-warhead-stockpiles.csv"))
stockpiles
```
The OWID data is already really tidy - no need to do much with the country names, and ISO3 codes are already provided. (There are also relatively few nuclear powers.)
The current nuclear powers are `r stockpiles %>% filter(Year == max(Year, na.rm = TRUE)) %>% pull(Entity) %>% unique() %>% glue_collapse(sep = ", ", last = " and ")`.
Let's get our world map boundaries:
```{r boundaries}
sf::sf_use_s2(FALSE)
ne_countries(returnclass = "sf") %>%
# geoboundaries() %>%
select(Code = iso_a3) -># %>%
# st_make_valid() ->
boundaries
sf::sf_use_s2(TRUE)
# get the centroids of the nuclear powers
boundaries %>%
filter(Code %in% unique(stockpiles$Code)) %>%
st_make_valid() %>%
st_centroid(of_largest_polygon = TRUE) ->
centroids
# now simplify the geometry for plotting
# boundaries %>%
# st_simplify(dTolerance = 1) ->
# simple_boundaries
```
Finally, we need to join the nuclear data with the centroids:
```{r joinnuclear}
centroids %>%
right_join(stockpiles, by = "Code") %>%
filter(nuclear_weapons_stockpile > 0) ->
stockpile_points
# finally, let's split based on large and small stockpiles so that we can tweak
# the label appearance
```
Okay, let's plot:
```{r map}
dir.create(here("out", "png"), showWarnings = FALSE)
dir.create(here("out", "svg"), showWarnings = FALSE)
si_count <- scales::label_number_si()
comma_count <- scales::label_comma(accuracy = 1)
warheads_map <- function(df, year) {
warhead_plot <- ggplot(df) +
# world map boundaries
geom_sf(data = boundaries, fill = "#cccccc", colour = NA) +
# points and labels
geom_sf(aes(size = Count, alpha = Count), colour = "orange") +
# labels: uk and france overlaps are handled manually to avoid jumping around (this would be way easier if gganimate was working)
geom_sf_text(
aes(label = pt_label),
data = df %>% filter(Entity %in% c("United Kingdom")),
family = "Body 360info", fontface = "bold",
size = 3,
# check_overlap = TRUE,
hjust = "right", nudge_x = -12.5,
colour = "black", show.legend = FALSE) +
geom_sf_text(
aes(label = pt_label),
data = df %>% filter(Entity %in% c("France")),
family = "Body 360info", fontface = "bold",
size = 3,
hjust = "left", nudge_x = 12.5,
colour = "black", show.legend = FALSE) +
# everyone else
geom_sf_text(
aes(label = pt_label),
data = df %>% filter(!(Entity %in% c("United Kingdom", "France"))),
family = "Body 360info", fontface = "bold",
size = 3,
# check_overlap = TRUE,
# hjust = 0, nudge_x = 0.5,
colour = "black", show.legend = FALSE) +
scale_alpha(
limits =
c(1, max(stockpile_points$nuclear_weapons_stockpile, na.rm = TRUE)),
range = c(1, 0.35), guide = NULL) +
scale_radius(name = "Num. warheads",
limits = c(1,
max(stockpile_points$nuclear_weapons_stockpile, na.rm = TRUE)),
range = c(1, 80), labels = si_count, guide = NULL) +
coord_sf(crs = "+proj=eck4", clip = "off") +
theme_360() +
theme(
plot.background = element_rect(fill = "white"),
legend.direction = "horizontal",
legend.position = "top",
plot.title = element_markdown(
colour = colours_360("lightblue"),
family = "Headline 360info",
size = rel(1.8),
margin = margin(b = 16 * 0.5))
) +
labs(
x = NULL, y = NULL,
title = paste0(
toupper("Estimated nuclear stockpiles in "),
"<span style='color:black'>",
year, "</span>"),
caption = paste(
"**CHART:** James Goldie & Reece Hooker, 360info",
"**DATA:** Our World In Data [ourworldindata.org/nuclear-weapons]",
sep = "<br>"))
register_360fonts("itc")
save_360plot(warhead_plot,
here("out", "png", paste0("warheads-", year, ".png")),
shape = "sdtv-landscape", retina = 4)
register_360fonts("libre")
save_360plot(warhead_plot,
here("out", "svg", paste0("warheads-", year, ".svg")),
shape = "sdtv-landscape")
return(warhead_plot)
}
# get the geometry coordinates
coordinates <- st_coordinates(stockpile_points)
# render each frame off
stockpile_points %>%
rename(Count = nuclear_weapons_stockpile) %>%
mutate(
x = coordinates[, 1],
y = coordinates[, 2],
pt_label = paste0(Entity, "\n",
if_else(Count >= 100, si_count(Count), as.character(Count)))) %>%
split(.$Year) %>%
imap(warheads_map) -> test
```
We can manually stitch this together in a video with `ffmpeg`:
```{r videoout}
system2("ffmpeg", c(
"-framerate", "3",
"-pattern_type", "glob", "-i", "'warheads-*.png'",
"-c:v", "libx264", "-pix_fmt", "yuv420p",
"warheads.mp4"))
```