-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecreates.Rmd
642 lines (565 loc) · 24.6 KB
/
recreates.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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
---
title: "Recreating figures from Storytelling with Data"
author: "Ashleigh Wilson"
date: '2022-02-24'
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(readxl)
library(janitor)
library(scales)
library(lemon)
library(ggtext)
library(grid)
library(gridExtra)
library(gridtext)
source("themes/my_swd_theme.R")
```
Reading in excel spreadsheet. Use the range argument to specify the exact cells to import.
```{r}
# meh, don't want to do this graph again.
# data_2_8_og <- read_excel("data/2.8_EXERCISE.xlsx", range = "B5:F15")
# data_2_8 <- clean_names(data_2_8_og)
# data_2_8[10,1] <- "Dec-19" # just changing value name
# data_2_8 <- data_2_8 %>%
# rename(year = x1) %>%
# mutate(year = as_factor(year))
```
------------------------------------------------------------------------
## Chapter 2 Figures
### 2.7
```{r}
data_0207_OG <- read_csv("data/fig_0207_data.csv")
```
```{r}
data_0207 <- clean_names(data_0207_OG)
```
Graph:
```{r}
data_0207 <- data_0207 %>%
mutate(
cost_per_mile = parse_number(cost_per_mile), # need to convert chars with dollar sign $ by using parse_number()
avg_miles = mean(miles_driven),
avg_cost = mean(cost_per_mile),
color = if_else(cost_per_mile < avg_cost, SILVER, DARKORANGE)
)
avg_miles2 <- mean(data_0207$miles_driven)
avg_cost2 <- mean(data_0207$cost_per_mile)
ggplot(data_0207) +
geom_point(aes(miles_driven, cost_per_mile, color = color), size = 2) +
scale_color_identity() + # can use when data has column values that represents aesthetics (which it does with the color column values)
scale_y_continuous(breaks = seq(0, 3, .5), limits = c(0, 3), labels = scales::dollar_format()) +
scale_x_continuous(limits = c(0, 4000), labels = scales::label_comma()) + # original was 1000 to 3000. Changing it also shrinks the plot area
geom_hline(yintercept = avg_cost2, linetype = "longdash") +
geom_point(aes(avg_miles2, avg_cost2), size = 4) +
geom_label(aes(avg_miles2, avg_cost2), label = "AVG", hjust = 1.25, size = 4.5, label.size = 0) +
labs(
title = "Cost per mile by miles driven",
y = "Cost per mile",
x = "Miles driven per month"
) +
my_swd_theme() +
coord_capped_cart(bottom = "right", left = "top") # this function won't work unless the theme I use has panel.border = element_blank() and axis.line = element_line()
```
### 2.11
```{r}
data_0211_OG <- read_csv("data/fig_0211_data.csv")
```
```{r}
data_0211_1 <- data_0211_OG %>%
rename(survey_category = ...1) %>%
pivot_longer(cols = c("2014", "2015"), names_to = "year", values_to = "percent") %>%
mutate(
percent = parse_number(percent),
# Relevel the categories to ensure that "Career development" is drawn last and displays on top of the other categories
survey_category = fct_relevel(factor(survey_category), "Peers", "Culture", "Work environment", "Leadership", "Rewards & recognition", "Perf management", "Career development"),
color = if_else(survey_category == "Career development", TOMATO, DIMGRAY)
)
# below code won't work to make the graph
data_0211_2 <- data_0211_OG %>%
rename(survey_category = ...1) %>%
mutate(
`2014` = parse_number(`2014`),
`2015` = parse_number(`2015`)
)
```
```{r}
ggplot(data_0211_1) +
# must make survey_category a factor before I can use as group aes
geom_line(aes(year, percent, group = survey_category, color = color), size = 1) +
geom_point(aes(year, percent, color = color), size = 3) +
scale_color_identity() + # always need this when I have color column with hex codes
scale_x_discrete(expand = expansion(1.9, 0), limits = c("2014", "2015"), labels = c(2014, 2015)) +
geom_text(aes(year, percent, label = paste0(percent, "%"), color = color), nudge_x = -.2, data = data_0211_1 %>% filter(year == 2014) %>% filter(survey_category != "Career development")) +
geom_text(aes(year, percent, label = paste0(percent, "%"), color = color), nudge_x = .2, data = data_0211_1 %>% filter(year == 2015) %>% filter(survey_category != "Career development")) +
geom_text(aes(year, percent, label = survey_category, color = color), nudge_x = -.4, data = data_0211_1 %>% filter(year == 2014) %>% filter(survey_category != "Career development"), hjust = 1) +
geom_text(aes(year, percent, label = paste0(percent, "%"), fontface = "bold", color = color), nudge_x = -.2, data = data_0211_1 %>% filter(year == 2014) %>% filter(survey_category == "Career development")) +
geom_text(aes(year, percent, label = paste0(percent, "%"), fontface = "bold", color = color), nudge_x = .2, data = data_0211_1 %>% filter(year == 2015) %>% filter(survey_category == "Career development")) +
geom_text(aes(year, percent, label = survey_category, color = color), fontface = "bold", nudge_x = -.4, data = data_0211_1 %>% filter(year == 2014) %>% filter(survey_category == "Career development"), hjust = 1) +
my_swd_theme() +
theme(
axis.line.y = element_blank(),
axis.line.x = element_line(size = .1),
axis.text.y = element_blank(),
axis.title.x = element_text(hjust = .5),
axis.title.y = element_blank(),
axis.ticks.y = element_blank(),
axis.ticks.x = element_line(color = LIGHTGRAY),
plot.title = element_text(hjust = .1, vjust = -4),
plot.subtitle = element_text(hjust = .208, vjust = -10, size = 10, color = DIMGRAY),
) +
labs(
title = "Employee feedback over time",
subtitle = "Survey category | Percent favorable",
x = "survey year"
) +
coord_capped_cart(bottom = "both") # makes the x axis line only connect from 2014 - 2015
```
### 2.19
```{r}
data_0219_OG <- read_csv("data/fig_0219_data.csv")
```
```{r}
data_0219 <- data_0219_OG %>%
rename(survey_item = ...1) %>%
clean_names() %>%
pivot_longer(cols = c("strongly_disagree":"strongly_agree"), names_to = "rating", values_to = "percent") %>%
mutate(
total = parse_number(total),
percent = parse_number(percent),
rating = fct_relevel(factor(rating), "strongly_disagree", "disagree", "neutral", "agree", "strongly_agree"),
rating = fct_rev(rating)
)
color_scale <- c(
"strongly_disagree" = DARKGRAY,
"disagree" = DARKGRAY,
"neutral" = LIGHTGRAY,
"agree" = BLUEGRAY,
"strongly_agree" = BLUEGRAY
)
```
```{r}
ggplot(data_0219, aes(x = percent, y = fct_rev(survey_item), fill = rating)) +
geom_col(color = "white", width = 0.8) +
scale_fill_manual(values = color_scale, guide = "none") +
scale_x_continuous(position = "top", labels = scales::label_percent(scale = 1)) +
my_swd_theme() +
theme(
axis.line.y = element_blank(),
axis.line.x = element_line(color = DIMGRAY, size = .2),
axis.title.y = element_blank(),
axis.title.x = element_text(color = DARKGRAY, size = 10, hjust = .035),
axis.text.y = element_text(hjust = 6, size = 10),
axis.ticks.y = element_blank(),
axis.ticks.x = element_line(size = .2, color = DIMGRAY),
plot.margin = margin(.2, .2, .2, .2, "cm"),
plot.title = element_text(hjust = .04, margin = margin(t = 0, r = 0, b = 10, l = 0, unit = "pt")),
plot.subtitle = element_markdown(hjust = .41, size = 10) # element_markdown is from the ggtext package. Allows me to set markdown text in plot (See subtitle).
) +
labs(
title = "Survey results",
subtitle = paste0(
"<span style='color:", color_scale[1], "'>", "**Strongly Disagree**", "</span>", " | ",
"<span style='color:", color_scale[2], "'>", "**Disagree**", "</span>", " | ",
"<span style='color:", color_scale[3], "'>", "**Neutral**", "</span>", " | ",
"<span style='color:", color_scale[4], "'>", "**Agree**", "</span>", " | ",
"<span style='color:", color_scale[5], "'>", "**Strongly Agree**", "</span>"
),
x = "Percent of total"
) +
coord_capped_cart(top = "both")
```
### 4.14
```{r}
data_0414_OG <- read_csv("data/fig_0414_data.csv")
```
```{r}
data_0414 <- data_0414_OG %>%
rename(ticket_type = ...1) %>%
pivot_longer(cols = -ticket_type, names_to = "month", values_to = "amount") %>%
mutate(
ticket_type = as_factor(ticket_type),
month = as_factor(month)
)
# the below is a grob object from the grid_text package. This was the only way to add a markdown annotation text box to the graph.
graph_annotation <- grobTree(richtext_grob(
"<span style='background_color:white'><b>2 employees quit in May.</b> Ticket volume dramatically <br> decreased in the following two months then increased after<br> hiring two new employees in July. Volume fluctuated in the<br> subsequent months.</span>",
x = .58, y = .93, gp = gpar(col = GRAY, fontsize = 12), hjust = 0, box_gp = gpar(col = "white", fill = "white"), padding = margin(.4, 0, 0, 0, "in")
))
```
Graph
```{r}
ggplot(data_0414, aes(x = month, y = amount, group = ticket_type, color = ticket_type)) +
geom_line(size = 1) +
geom_point(size = 2.5, data = data_0414 %>% filter(month %in% c("Aug", "Sep", "Oct", "Nov", "Dec"))) + # seems I can use data arg in any geom to filter for specific data to be shown
geom_vline(xintercept = "May", color = GRAY) + # the grob textbox will cover the top part of the vline, giving it that 'cutoff' appearance.
# geom_linerange(x = "May", ymin = 0, ymax = 240) +
scale_color_manual(values = c(GRAY, BLUEGRAY)) +
scale_y_continuous(limits = c(0, 300), breaks = seq(0, 300, 50)) +
scale_x_discrete(expand = expansion(add = c(.02, 3))) +
geom_text(aes(label = amount), nudge_y = 18, data = data_0414 %>% filter(ticket_type != "Ticket Volume Processed") %>% filter(month %in% c("Aug", "Sep", "Oct", "Nov", "Dec"))) +
geom_text(aes(label = amount), nudge_y = -18, data = data_0414 %>% filter(ticket_type != "Ticket Volume Received") %>% filter(month %in% c("Aug", "Sep", "Oct", "Nov", "Dec"))) +
annotate("text", x = 13.5, y = 180, size = 5, color = GRAY, label = "bold(Received)", parse = TRUE) +
annotate("text", x = 13.68, y = 142, size = 5, color = BLUEGRAY, label = "bold(Processed)", parse = TRUE) +
annotation_custom(graph_annotation, xmin = -9) + # used with custom grob
coord_capped_cart(bottom = "right", left = "top") +
labs(
title = "Ticket volume over time"
) +
my_swd_theme() +
theme(
legend.position = "none",
plot.margin = margin(t = .5, r = .5, b = .5, l = .5, unit = "cm"),
plot.background = element_rect(color = "white"),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.line = element_line(size = .2),
axis.ticks.x = element_blank(),
)
```
### 5.4
```{r}
data_0504_OG <- read_csv("data/fig_0504_data.csv")
```
```{r}
data_0504 <- data_0504_OG %>%
pivot_longer(cols = "2008":"2012", names_to = "year", values_to = "number") %>%
filter(Category != "All") %>%
mutate(
Category = as_factor(Category),
color = if_else(Category == "Bachelor's degree or more", ORANGE, SILVER)
)
```
Graph
```{r}
ggplot(data_0504, aes(year, number, group = Category, color = color)) +
geom_line(size = 1.5) +
geom_point(size = 3, data = data_0504 %>% filter(year %in% c("2008", "2012"))) +
scale_color_identity() +
scale_x_discrete(expand = expansion(add = c(5, 1))) +
scale_y_continuous(limits = c(5, 65)) + # setting limits can `zoom-in or out` of the plot, so to speak. Gives me more space below the below most line.
geom_text(aes(label = number), size = 6.5, nudge_x = .3, nudge_y = .4, data = data_0504 %>% filter(year == "2012") %>% mutate(number = round(number))) +
geom_text(aes(label = number), size = 6.5, nudge_x = -.15, nudge_y = .4, hjust = 1, data = data_0504 %>% filter(year == "2008") %>% mutate(number = round(number))) +
geom_text(aes(label = Category), size = 6, nudge_x = -.7, nudge_y = .2, hjust = 1, data = data_0504 %>% filter(year == "2008", Category != "Bachelor's degree or more")) + # hjust will line the Categories neatly
geom_text(aes(label = Category), size = 6, nudge_x = -.7, nudge_y = .4, hjust = 1, data = data_0504 %>% filter(year == "2008", Category == "Bachelor's degree or more")) +
labs(
title = "New marriage rate by education",
subtitle = paste0("<span>Number of <b>newly</b> married adults per 1,000 marriage eligible adults</span>"),
caption = "Note: Marriage eligibility includes newly married plus those widowed or divorced.\nSource: US Census\nAdapted from PEW RESEARCH CENTER"
) +
coord_capped_cart(bottom = "both") + # I think this needs to always be at the bottom of the ggplot call.
my_swd_theme() +
theme(
axis.title.y = element_blank(),
axis.title.x = element_blank(),
axis.line.y = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
plot.subtitle = element_markdown(size = 10)
)
```
### 5.13
```{r}
data_0513_OG <- read_csv("data/fig_0513_data.csv")
```
```{r}
data_0513 <- data_0513_OG %>%
pivot_longer(cols = c(`US Population`, `Our Customers`), names_to = "customer_type", values_to = "number") %>%
mutate(
Segment = as_factor(Segment),
customer_type = as_factor(customer_type),
num_color = case_when(
Segment == "Segment 3" ~ WHITE,
Segment == "Segment 4" ~ WHITE,
Segment == "Segment 5" ~ WHITE,
TRUE ~ BLACK
)
)
data_0513_color_values <- c(
"Segment 1" = SILVER,
"Segment 2" = SILVER,
"Segment 3" = BLUEGRAY,
"Segment 4" = BLUEGRAY,
"Segment 5" = BLUEGRAY,
"Segment 6" = SILVER,
"Segment 7" = SILVER
)
percent_30_grob <- grobTree(richtext_grob(
"<b>30%</b>",
gp = gpar(fontsize = 20, col = BLUEGRAY)
))
percent_50_grob <- grobTree(richtext_grob(
"<b>50%</b>",
gp = gpar(fontsize = 20, col = BLUEGRAY)
))
# reference to the below solutions (line_*_grob): https://rdrr.io/r/grid/grid.segments.html
line_30_grob <- segmentsGrob(
x0 = unit(.43, "npc"),
y0 = unit(.525, "npc"),
x1 = unit(.43, "npc"),
y1 = unit(.258, "npc"),
default.units = "npc",
arrow = NULL, name = NULL, gp = gpar(), vp = NULL
)
line_50_grob <- segmentsGrob(
x0 = unit(.865, "npc"),
y0 = unit(.67, "npc"),
x1 = unit(.865, "npc"),
y1 = unit(.22, "npc"),
default.units = "npc",
arrow = NULL, name = NULL, gp = gpar(), vp = NULL
)
```
Graph
```{r}
ggplot(data_0513, aes(customer_type, number, fill = Segment)) +
# scale_fill_identity() +
scale_fill_manual(values = data_0513_color_values) +
geom_col(color = "white", width = .5) +
scale_x_discrete(expand = expansion(add = c(.7, .6))) +
geom_text(aes(label = Segment, color = Segment), size = 4.1, position = position_stack(vjust = .55), hjust = 2.2, data = data_0513 %>% filter(customer_type == "US Population")) +
scale_color_manual(values = data_0513_color_values) +
geom_text(aes(label = paste0(number * 100, "%"), parse = TRUE), color = WHITE, position = position_stack(vjust = .6)) +
annotation_custom(percent_30_grob, xmin = .2, ymin = -.29) +
annotation_custom(percent_50_grob, xmin = 2.2, ymin = -.2) +
# geom_linerange(x = "US Population", ymin = 0, ymax = .52, size = 6) +
annotation_custom(line_30_grob) +
annotation_custom(line_50_grob) +
labs(
title = "Distribution by customer segment"
) +
my_swd_theme() +
theme(
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.line.y = element_blank(),
axis.ticks.y = element_blank(),
axis.text.y = element_blank()
)
```
### 6.1
```{r}
data_0601_OG <- read_csv("data/fig_0601_data.csv")
```
```{r}
data_0601 <- data_0601_OG %>%
clean_names() %>%
mutate(
last_year = parse_number(last_year),
progress_to_date = parse_number(progress_to_date)
) %>%
pivot_longer(cols = c(last_year, progress_to_date), names_to = "money_raised_type", values_to = "amount")
color_scale_0601 <- c(
"last_year" = LIGHTSTEELBLUE,
"progress_to_date" = BLUEGRAY
)
progress_grob_0601 <- grobTree(richtext_grob(
"<b>$33,967</b>",
gp = gpar(col = BLUEGRAY, fontsize = 12)
))
last_year_grob_0601 <- grobTree(richtext_grob(
"<b>$51,400</b>",
gp = gpar(col = LIGHTSTEELBLUE, fontsize = 8)
))
```
Graph
```{r}
ggplot(data_0601, aes(days_since_launch, amount, color = money_raised_type, group = money_raised_type)) +
scale_color_manual(values = color_scale_0601) +
geom_line() +
geom_point(data = data_0601 %>% filter(money_raised_type == "progress_to_date" & days_since_launch == 10)) +
geom_point(data = data_0601 %>% filter(money_raised_type == "last_year" & days_since_launch == 30)) +
scale_y_continuous(limits = c(0, 60000), breaks = seq(0, 60000, 10000), labels = label_dollar()) +
scale_x_continuous(breaks = seq(0, 30, 5), expand = expansion(add = c(.1, 3))) +
# geom_hline(yintercept = 50000, color = GRAY) +
geom_linerange(y = 50000, xmin = 0, xmax = 30, color = GRAY, size = .2) +
annotate("text", x = 10.25, y = 39000, label = "Progress to date", color = BLUEGRAY, size = 4.5) +
annotate("text", x = 31.7, y = 54800, label = "Last year", color = LIGHTSTEELBLUE, size = 3) +
annotation_custom(progress_grob_0601, xmin = -10.1, ymin = 5000) +
annotation_custom(last_year_grob_0601, xmin = 29, ymin = 40000) +
geom_label(x = 2, y = 50000, label = "GOAL", fill = "white", label.size = NA, color = GRAY, size = 3) +
labs(
title = "Annual giving campaign progress",
x = "Days since campaign launch",
y = "Money raised"
) +
my_swd_theme() +
theme(
axis.ticks.y = element_line(size = .2),
axis.ticks.x = element_line(size = .2),
plot.title = element_text(hjust = .08)
) +
coord_capped_cart(bottom = "right", left = "top")
```
## Practice Workbook Figures
### 7.1
```{r}
data_pract_0701_OG <- read_csv("data/fig_practice_0701_data.csv")
```
```{r}
data_pract_0701 <- data_pract_0701_OG %>%
pivot_longer(cols = `2015`:`2019`, names_to = "year", values_to = "percent") %>%
mutate(
percent = parse_number(percent),
year = as_factor(year)
)
blue_text_grob <- grobTree(richtext_grob(
"<span style=\"fontsize: 1em\">The proportion of revenue from Feature <em>Z</em> is higher sooner:<br><span style=\"color:DARKBLUE\">28% of revenue was generated through Feature <em>Z</em> within<br> the first quarter advertising for new advertisers in 2019<br></span> compared to 15% for those creating<br> their first ad in 2015.</span>",
gp = gpar(fontsize = 8), hjust = 0, align_widths = TRUE, padding = unit(c(6, 6, 4, 6), "pt")
))
green_text_grob <- grobTree(richtext_grob(
"<span style=\"fontsize: 1em\"><br><br>The proportion of revenue from Feature <em>Z</em> generally<br>increases with advertiser tenure. <span style=\"color:FORESTGREEN\">By 16 quarters of<br>advertising, 50% of revenue came from Feature <em>Z</em><br>for those first creating an ad in 2015.</span> Given the<br>increase in sophistication over time, this may end<br>up being even higher for more recent cohorts.<br><br><br><br><br>We will continue to monitor and<br>report back if things change.</span>",
gp = gpar(fontsize = 8), hjust = 1, align_widths = TRUE, box_gp = gpar(col = LIGHTSTEELBLUE, fill = NA, lwd = 2), padding = unit(c(4, 4, 2, 4), "pt")
))
```
Graph
```{r}
ggplot(data_pract_0701, aes(Age, percent, group = year)) +
geom_line(color = GRAY, size = .8) +
geom_point(color = GRAY, size = 3, data = data_pract_0701 %>% filter(Age == 0 & year != "2019")) +
geom_point(color = DARKBLUE, size = 3, data = data_pract_0701 %>% filter(Age == 0 & year == "2019")) +
geom_point(color = GREEN, size = 3, data = data_pract_0701 %>% filter(year == "2015" & Age == 15)) +
scale_y_continuous(limits = c(0, 50), labels = label_percent(scale = 1), expand = expansion(add = c(1, 3))) +
scale_x_continuous(breaks = seq(0, 16, 1), expand = expansion(add = c(.01, 2))) +
geom_text(aes(label = year), color = GREEN, nudge_x = .8, data = data_pract_0701 %>% filter(year == "2015" & Age == 15)) +
geom_text(aes(x = 11.75, y = 43.8, label = year), color = GRAY, data = data_pract_0701 %>% filter(year == "2016" & Age == 12)) +
geom_text(aes(x = 8.55, y = 40, label = year), color = GRAY, data = data_pract_0701 %>% filter(year == "2017" & Age == 12)) +
geom_text(aes(x = 4.6, y = 34.6, label = year), color = GRAY, data = data_pract_0701 %>% filter(year == "2018" & Age == 12)) +
geom_text(aes(x = 1, y = 33, label = year), color = DARKBLUE, data = data_pract_0701 %>% filter(year == "2019" & Age == 12)) +
annotation_custom(blue_text_grob, xmin = -20.2, ymin = 33) +
annotate("text", label = "bold(Sophistication~increases~with~time)", x = 4.5, y = 50, parse = TRUE) +
annotate("text", label = "bold(Sophistication~increases~with~age)", hjust = 0.45, x = 13.4, y = 29, parse = TRUE) +
annotation_custom(green_text_grob, xmin = 16, ymin = -21) +
annotate("text", label = "bold(No~action~is~needed~at~this~time)", x = 14.1, y = 7, parse = TRUE) +
annotate("text", label = paste("FIRST AD", "CREATED", sep = "\n"), size = 2.5, hjust = 1, x = 18.2, y = 50, color = GRAY) +
labs(
title = "Feature Z % of revenue by advertiser age",
x = "ADVERTISER AGE (QTRS)",
y = "FEATURE Z % OF TOTAL REVENUE"
) +
my_swd_theme() +
theme(
axis.title.y = element_text(size = 9),
axis.title.x = element_text(size = 9)
) +
coord_capped_cart(bottom = "right", left = "top")
```
### Case study #5
```{r}
pilot_data_OG <- read_csv("data/pilot_program_data.csv")
```
```{r}
pilot_data_2 <- pilot_data_OG %>%
pivot_longer(-program_type, names_to = "item_type", values_to = "item_value") %>%
mutate(
program_type = str_to_upper(program_type)
) %>%
mutate(
program_type = as_factor(program_type),
item_value = parse_number(item_value),
item_type = fct_rev(as_factor(item_type))
)
pilot2_color_scale <- c(
"Bored" = GRAY,
"Not Great" = GRAY,
"OK" = ORANGE,
"Kind of interested" = STEELBLUE,
"Excited" = STEELBLUE
)
pilot_left_grob <- grobTree(richtext_grob(
"<span style=\"fontsize:5rem; color:DIMGRAY\">BEFORE program, the<br><span style=\"color:ORANGE\">majority of children (40%)<br>felt just *OK*</span> about science.</span>",
hjust = 0
))
pilot_right_grob <- grobTree(richtext_grob(
"<span style=\"fontsize:5rem; color:DIMGRAY\">AFTER program, <span style=\"color:STEELBLUE\">more children<br>were *Kind of interested (30%)*<br>& *Excited (38%)*</span> about science.</span>",
hjust = 1
))
```
Graph
```{r}
ggplot(pilot_data_2, aes(x = fct_rev(program_type), y = item_value, fill = item_type)) +
geom_col(color = "white", width = .6) +
scale_fill_manual(values = pilot2_color_scale) +
scale_y_continuous(position = "right", labels = label_percent(scale = 1)) +
scale_x_discrete(expand = expansion(add = c(1, .4))) +
coord_flip() +
my_swd_theme() +
theme(
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.line.y = element_blank(),
axis.ticks.y = element_blank(),
plot.subtitle = element_markdown(hjust = .21, size = 12, padding = unit(c(.5, 0, .5, 0), "cm"))
) +
labs(
title = "How do you feel about science?",
subtitle = paste0(
"<span style='color:", pilot2_color_scale[1], "'>", "**Bored**", "</span>", " | ",
"<span style='color:", pilot2_color_scale[2], "'>", "**Not Great**", "</span>", " | ",
"<span style='color:", pilot2_color_scale[3], "'>", "**OK**", "</span>", " | ",
"<span style='color:", pilot2_color_scale[4], "'>", "**Kind of interested**", "</span>", " | ",
"<span style='color:", pilot2_color_scale[5], "'>", "**Excited**", "</span>"
)
) +
annotation_custom(pilot_left_grob, xmax = .6, ymax = 5) +
annotation_custom(pilot_right_grob, xmax = .6, ymax = 205)
```
## Random
```{r}
div_OG <- read_csv("data/hr_diversity.csv")
```
```{r}
div_OG %>%
select(Department, Salary, RaceDesc) %>%
group_by(Department) %>%
count(RaceDesc)
```
```{r}
diversity_colors <- c(
"White" = BLUEGRAY,
"Black or African American" = LIGHTSTEELBLUE,
"Two or more races" = STEELBLUE,
"Asian" = GRAY,
"American Indian or Alaska Native" = SILVER,
"Hispanic" = NAVY
)
perc_group <- grobTree(richtext_grob(
"<b>$51,400</b>",
gp = gpar(col = LIGHTSTEELBLUE, fontsize = 8)
))
```
```{r}
div_OG %>%
select(Department, Salary, RaceDesc) %>%
mutate(across(where(is.character), as_factor)) %>%
group_by(Department, RaceDesc) %>%
summarise(n = n()) %>%
mutate(
RaceDesc = fct_rev(RaceDesc),
perc = round(n / sum(n), digits = 2)
) %>%
ggplot() +
geom_col(mapping = aes(x = perc, y = Department, fill = RaceDesc), color = "white", width = .9) +
# scale_fill_brewer(palette = "Paired")
scale_fill_manual(values = diversity_colors) +
scale_x_continuous(position = "top", expand = expansion(add = c(0, .05)), labels = label_percent()) +
my_swd_theme() + theme(
legend.position = "top",
legend.justification = c(0,1),
legend.title = element_blank(),
legend.text = element_text(size = 13),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.line.y = element_blank(),
axis.ticks.y = element_blank(),
axis.text.y = element_text(size = 12),
plot.title = element_text(margin = margin(1,1,15,1, unit = "pt"), size = 20),
plot.background = element_rect(color = "white")
) +
labs(
title = "Distribution of employees by Department and Race",
) +
coord_capped_cart(top = "both")
```