-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path27 Replicate NYT figure using ggplot2.R
More file actions
353 lines (277 loc) · 12.7 KB
/
27 Replicate NYT figure using ggplot2.R
File metadata and controls
353 lines (277 loc) · 12.7 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# Replicating NY plot
# Youtube video: Riffomonas Project: "Recreating a New York Times bar chqart using ggplot package"(CC332)
# https://www.youtube.com/watch?v=8_2frsX5MwM
# File: 27 replicate NYT figure using ggplot2.R
# 1. Load required libraries
library(tidyverse)
library(tibble) ## To use tibble function
flooding <- tibble(
year = 2000:2024,
declarations = c(2,5,4,0,1,2,2,1,3,3,9,25,
3,17,10,9,18,12,17,53,21,10,
14,42,66),
is_2024 = year == 2024 # This will create a flag TRUE only for 2024
)
flooding
# 2. Create a bar plot
# using geom_col() generated the bar plot
# 2.1 Basig bar plot uisng geom_col() function
Plot_01 <- flooding %>%
ggplot(aes(x=year, y = declarations)) +
geom_col()
Plot_01
ggsave("01_Standard_bar_plot_geom_col.png", width = 6.38, height = 5.80)
# 2.2 Colours
# Using colour picker add in we choose these three colours:
# c("turquoise3", "deepskyblue2", "dodgerblue3")
# From youtube video
# # #BAD1D6 > Aquamarine colour
Plot_02 <- flooding %>%
ggplot(aes(x=year, y = declarations)) +
geom_col(fill = "#BAD1D6")
Plot_02
ggsave("02_Aquamarine colour_plot_geom_col.png", width = 6.38, height = 5.80)
# 2.3 This plot just displays two default colour for True and False values of "is_2024" variable.
Plot_03 <- flooding %>%
ggplot(aes(x=year, y = declarations, fill = is_2024)) +
geom_col()
Plot_03
ggsave("03_True_false_colour_defined_by_is_2024_variable.png", width = 6.38, height = 5.80)
# 2.4 Start designing last year different colour for 2024
# using scale_fill_manual()
# colours: FALSE values for "is_2024" variable: Aquamarine "#BAD1D6"
# TRUE values for "is_2024" variable: Last year is 2024. DARK BLUE "#539CBA"
Plot_04 <- flooding %>%
ggplot(aes(x=year, y = declarations, fill = is_2024)) +
geom_col(show.legend = FALSE) +
scale_fill_manual(breaks = c(FALSE,TRUE),
values = c("#BAD1D6","#539CBA"))
Plot_04
ggsave("04_Flagged_2024_year_colour.png", width = 6.38, height = 5.80)
# 2.5 Apply specific theme to apply theme clasic theme to the plot
# theme_classic()
Plot_05 <- flooding %>%
ggplot(aes(x=year, y = declarations, fill = is_2024)) +
geom_col(show.legend = FALSE) +
scale_fill_manual(breaks = c(FALSE,TRUE),
values = c("#BAD1D6","#539CBA")) +
theme_classic()
Plot_05
ggsave("05_Flagged_2024_year_colour_theme_classic.png", width = 6.38, height = 5.80)
# 2.6 Remove expansion around the whole perimeter
# Always remove this expansion for geom_col()
# coord_cartesian(expand = FALSE) +
Plot_06 <- flooding %>%
ggplot(aes(x=year, y = declarations, fill = is_2024)) +
geom_col(show.legend = FALSE) +
scale_fill_manual(breaks = c(FALSE,TRUE),
values = c("#BAD1D6","#539CBA")) +
coord_cartesian(expand = FALSE) +
theme_classic()
Plot_06
ggsave("06_Flagged_2024_year_colour_theme_classic_no_gap.png", width = 6.38, height = 5.80)
# 2.7 Include title and caption
# Also fit legend using element_textbox_simple() from library(ggtext)
library(ggtext)
Plot_07 <- flooding %>%
ggplot(aes(x=year, y = declarations, fill = is_2024)) +
geom_col(show.legend = FALSE) +
scale_fill_manual(breaks = c(FALSE,TRUE),
values = c("#BAD1D6","#539CBA")) +
coord_cartesian(expand = FALSE) +
labs(title = "A surge in U.S. flood disasters",
caption = "Note: The 2024 total reflects declarations os of Oct. 22, 2024 * Source: Federal Emergency Management Agency * By The New York Times") +
theme_classic() +
theme(
plot.caption = element_textbox_simple(), # Wrap legend text
plot.caption.position = "plot", # Caption and title left aligned
plot.title.position = "plot"
)
Plot_07
ggsave("07_Flagged_2024_year_colour_theme_title_caption.png", width = 6.38, height = 5.80)
# 2.8 Format title as bold
library(ggtext)
Plot_08 <- flooding %>%
ggplot(aes(x=year, y = declarations, fill = is_2024)) +
geom_col(show.legend = FALSE) +
scale_fill_manual(breaks = c(FALSE,TRUE),
values = c("#BAD1D6","#539CBA")) +
coord_cartesian(expand = FALSE) +
labs(title = "A surge in U.S. flood disasters",
caption = "Note: The 2024 total reflects declarations os of Oct. 22, 2024 * Source: Federal Emergency Management Agency * By The New York Times") +
theme_classic() +
theme(
plot.title = element_text(face = "bold"),
plot.caption = element_textbox_simple(), # Wrap legend text
plot.caption.position = "plot", # Caption and title left aligned
plot.title.position = "plot"
)
Plot_08
ggsave("08_Flagged_2024_title_caption_title_bold.png", width = 6.38, height = 5.80)
# 2.9 Create annotation bubble
# we will use {ggtext} to include new fonts
# Using break text with <br>
# geom_richtext(geom = "textbox", label = "66 declarations<br>this year so far",
# x = 2022.5, y = 62,
# hjust = 1,
# show.legend = FALSE)
library(ggtext)
Plot_09 <- flooding %>%
ggplot(aes(x=year, y = declarations, fill = is_2024)) +
geom_col(show.legend = FALSE) +
geom_richtext(geom = "textbox", label = "66 declarations<br>this year so far",
x = 2022.5, y = 62,
hjust = 1,
show.legend = FALSE) +# annotation bubble
scale_fill_manual(breaks = c(FALSE,TRUE),
values = c("#BAD1D6","#539CBA")) +
coord_cartesian(expand = FALSE) +
labs(title = "A surge in U.S. flood disasters",
caption = "Note: The 2024 total reflects declarations os of Oct. 22, 2024 * Source: Federal Emergency Management Agency * By The New York Times") +
theme_classic() +
theme(
plot.title = element_text(face = "bold"),
plot.caption = element_textbox_simple(), # Wrap legend text
plot.caption.position = "plot", # Caption and title left aligned
plot.title.position = "plot"
)
Plot_09
ggsave("09_Flagged_2024_title_bold_annotation_bubble.png", width = 6.38, height = 5.80)
# 2.10 Modiffy initial annotation bubble created earlier
# New parameters introduced to geom_richtext() function
# fill = NA,
# color = "black",
Plot_10 <- flooding %>%
ggplot(aes(x=year, y = declarations, fill = is_2024)) +
geom_col(show.legend = FALSE) +
geom_richtext(geom = "textbox", label = "66 declarations<br>this year so far",
x = 2022.5, y = 62,
hjust = 1,
fill = NA,
color = "black",
show.legend = FALSE) +# annotation bubble
scale_fill_manual(breaks = c(FALSE,TRUE),
values = c("#BAD1D6","#539CBA")) +
coord_cartesian(expand = FALSE) +
labs(title = "A surge in U.S. flood disasters",
caption = "Note: The 2024 total reflects declarations os of Oct. 22, 2024 * Source: Federal Emergency Management Agency * By The New York Times") +
theme_classic() +
theme(
plot.title = element_text(face = "bold"),
plot.caption = element_textbox_simple(), # Wrap legend text
plot.caption.position = "plot", # Caption and title left aligned
plot.title.position = "plot"
)
Plot_10
ggsave("10_Flagged_2024_title_bold_annotation_bubble_formatted.png", width = 6.38, height = 5.80)
# 2.11 Adding the colouring emphasis for the 66 declarations
# Introducing HTML and CSS
# "<span style = 'color:#539CBA'>66 declarations</span>
Plot_11 <- flooding %>%
ggplot(aes(x=year, y = declarations, fill = is_2024)) +
geom_col(show.legend = FALSE) +
geom_richtext(geom = "textbox", label = "<span style='color:#539CBA'>66 declarations</span><br>this year so far",
x = 2022.5, y = 62,
hjust = 1,
fill = NA,
color = "black",
show.legend = FALSE) +# annotation bubble
scale_fill_manual(breaks = c(FALSE,TRUE),
values = c("#BAD1D6","#539CBA")) +
coord_cartesian(expand = FALSE) +
labs(title = "A surge in U.S. flood disasters",
caption = "Note: The 2024 total reflects declarations os of Oct. 22, 2024 * Source: Federal Emergency Management Agency * By The New York Times") +
theme_classic() +
theme(
plot.title = element_text(face = "bold"),
plot.caption = element_textbox_simple(), # Wrap legend text
plot.caption.position = "plot", # Caption and title left aligned
plot.title.position = "plot"
)
Plot_11
ggsave("11_Flagged_2024_title_include_colour_to_text_bubble.png", width = 6.38, height = 5.80)
# 2.12 Make the sentence "66 declarations" bold using markdown **
# geom_richtext(geom = "textbox", label = "<span style='color:#539CBA'>**66 declarations**</span><br>this year so far",
Plot_12 <- flooding %>%
ggplot(aes(x=year, y = declarations, fill = is_2024)) +
geom_col(show.legend = FALSE) +
geom_richtext(geom = "textbox", label = "<span style='color:#539CBA'>**66 declarations**</span><br>this year so far",
x = 2022.5, y = 62,
hjust = 1,
fill = NA,
color = "black",
show.legend = FALSE) +# annotation bubble
scale_fill_manual(breaks = c(FALSE,TRUE),
values = c("#BAD1D6","#539CBA")) +
coord_cartesian(expand = FALSE) +
labs(title = "A surge in U.S. flood disasters",
caption = "Note: The 2024 total reflects declarations os of Oct. 22, 2024 * Source: Federal Emergency Management Agency * By The New York Times") +
theme_classic() +
theme(
plot.title = element_text(face = "bold"),
plot.caption = element_textbox_simple(), # Wrap legend text
plot.caption.position = "plot", # Caption and title left aligned
plot.title.position = "plot"
)
Plot_12
ggsave("12_Flagged_2024_title_include_colour_to_text_bubble_bold.png", width = 6.38, height = 5.80)
# 2.13 Remove border around "66 declarations" text annotation
# label.colour = NA,
Plot_13 <- flooding %>%
ggplot(aes(x=year, y = declarations, fill = is_2024)) +
geom_col(show.legend = FALSE) +
geom_richtext(geom = "textbox", label = "<span style='color:#539CBA'>**66 declarations**</span><br>this year so far",
x = 2022.5, y = 62,
hjust = 1,
fill = NA,
color = "black",
label.colour = NA,
show.legend = FALSE) +# annotation bubble
scale_fill_manual(breaks = c(FALSE,TRUE),
values = c("#BAD1D6","#539CBA")) +
coord_cartesian(expand = FALSE) +
labs(title = "A surge in U.S. flood disasters",
caption = "Note: The 2024 total reflects declarations os of Oct. 22, 2024 * Source: Federal Emergency Management Agency * By The New York Times") +
theme_classic() +
theme(
plot.title = element_text(face = "bold"),
plot.caption = element_textbox_simple(), # Wrap legend text
plot.caption.position = "plot", # Caption and title left aligned
plot.title.position = "plot"
)
Plot_13
ggsave("13_Flagged_2024_title_include_colour_to_text_bubble_bold_no_border.png", width = 6.38, height = 5.80)
# 2.14 Draw horizontal line from text to bar
# DRAW HORIZONTAL LINE FROM THE TEXT TO THE BAR
# using annotate() function after geom_richtext() function
# Placing annotate() BEFORE geom_col()
# Horizontal line position:
# annotate(geom = "segment",x = 2022.5 , xend = 2023.4,y = 63, yend = 63)
Plot_14 <- flooding %>%
ggplot(aes(x=year, y = declarations, fill = is_2024)) +
annotate(geom = "segment",
x = 2022.5 , xend = 2023.4,
y = 63, yend = 63) + # It requires an x,xend,y, yend
geom_col(show.legend = FALSE) +
geom_richtext(geom = "textbox", label = "<span style='color:#539CBA'>**66 declarations**</span><br>this year so far",
x = 2022.5, y = 62,
hjust = 1,
fill = NA,
color = "black",
label.colour = NA,
show.legend = FALSE) +# annotation bubble
scale_fill_manual(breaks = c(FALSE,TRUE),
values = c("#BAD1D6","#539CBA")) +
coord_cartesian(expand = FALSE) +
labs(title = "A surge in U.S. flood disasters",
caption = "Note: The 2024 total reflects declarations os of Oct. 22, 2024 * Source: Federal Emergency Management Agency * By The New York Times") +
theme_classic() +
theme(
plot.title = element_text(face = "bold"),
plot.caption = element_textbox_simple(), # Wrap legend text
plot.caption.position = "plot", # Caption and title left aligned
plot.title.position = "plot"
)
Plot_14
ggsave("14_Flagged_2024_colour_to_text_bubble_bold_no_border_LINE_TO_BAR.png", width = 6.38, height = 5.80)
# 2.15 MODIFY AXIS TICKS (WIP)