-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmonster_types.R
144 lines (136 loc) · 3.64 KB
/
monster_types.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
# Setup -------------------------------------------------------------------
librarian::shelf(tidyverse, systemfonts, ggtext, ragg)
scoobydoo <-
readr::read_csv(
'https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-07-13/scoobydoo.csv',
na = 'NULL'
)
# Tidy --------------------------------------------------------------------
tidy_scooby <-
scoobydoo %>%
pivot_longer(
starts_with('caught'),
names_to = 'caught_by',
names_prefix = 'caught_',
values_to = 'caught'
) %>%
mutate(caught_by = snakecase::to_title_case(caught_by)) %>%
separate_rows(monster_type, sep = ',', convert = TRUE) %>%
drop_na(monster_type) %>%
filter(monster_type != '') %>%
mutate(
across(monster_type,
~ str_trim(.x) %>% as_factor),
monster_type = recode(monster_type,
Possessed = "Possessed Object",
Disguise = "Disguised")
)
monsters_sum <-
tidy_scooby %>%
select(monster_type, caught_by, caught) %>%
filter(caught_by != 'none') %>%
mutate(across(ends_with('type'),
fct_lump_n,
n = 10)) %>%
group_by(monster_type) %>%
count(wt = caught, sort = TRUE) %>%
ungroup() %>%
mutate(
monster_type = fct_rev(fct_inorder(monster_type)),
monster_type = forcats::fct_relevel(monster_type, "Other", after = 0L),
perc = scales::percent(n / sum(n), accuracy = .1, trim = FALSE)
)
# Visualize ---------------------------------------------------------------
monsters_sum <-
monsters_sum %>%
mutate(
color = case_when(
# Blue
row_number() == 1 ~ "#003A79",
# Orange
row_number() == 2 ~ "#F26D00",
# Yellow
row_number() == 3 ~ "#F5CC00",
# Light grey
monster_type == "Other" ~ "#D0D3D4",
## Darker grey
TRUE ~ "#666666"
)
)
monsters_sum %>%
ggplot(aes(x = monster_type ,
y = n,
fill = color)) +
geom_col() +
geom_label(
aes(label = perc),
hjust = 1,
nudge_y = -2.5,
size = 4,
fontface = "bold",
family = "Roboto",
## Box without outline
fill = "#FAFAFA",
label.size = 0,
color = "#003A79"
) +
scale_y_continuous(expand = c(.01, .01)) +
scale_fill_identity(guide = "none") +
theme_void() +
theme(
text =
element_text(
family = "Roboto",
face = "plain",
colour = "black",
size = 16,
lineheight = 0.9,
hjust = 0.5,
vjust = 0.5,
angle = 0,
margin = margin(),
debug = FALSE
),
plot.title = ggtext::element_textbox_simple(
# font size "large"
size = rel(1.2),
color = "#003A79",
face = "bold",
hjust = 0,
vjust = 1,
margin = margin(b = 8)
),
plot.title.position = "plot",
plot.subtitle = ggtext::element_textbox_simple(
# font size "regular"
hjust = 0,
vjust = 1,
margin = margin(b = 8)
),
plot.caption = ggtext::element_textbox_simple(
# font size "small"
size = rel(0.8),
vjust = 1,
family = "Roboto Light",
color = "#666666",
hjust = 0,
margin = margin(t = 8)
),
plot.caption.position = "plot",
axis.text.y = element_text(size = 14, hjust = 1),
plot.margin = margin(rep(15, 4)),
panel.background = element_rect(color = "#FAFAFA")
) +
coord_flip() +
labs(title = "Scooby Scooby *Boo!*",
subtitle = "Top 10 monsters",
caption = "**Source**: Kaggle")
ggsave(
here::here('figures', '03-scooby-doo-monsters.png'),
width = 85 * (14 / 5),
height = 53 * (14 / 5),
units = 'mm',
dpi = 300,
type = 'cairo',
device = agg_png()
)