-
Notifications
You must be signed in to change notification settings - Fork 2
/
tidytuesday_20210713.R
52 lines (48 loc) · 2.4 KB
/
tidytuesday_20210713.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
# US Scooby Doo
# TidyTuesday 2021 week 29
# Rebecca Stevick updated 8/3/2021
# Load libraries -----------------
library(tidyverse)
library(hrbrthemes)
# Load data ----------------------
tuesdata <- tidytuesdayR::tt_load('2021-07-13')
scoobydoo <- tuesdata$scoobydoo
# Analysis and plotting ----------
scoobydoo %>%
# probably the long way to do this... but get all the data organized.
select(index, caught_fred:unmask_scooby) %>%
pivot_longer(caught_fred:caught_scooby, names_to="caught", values_to="valuecaught") %>%
pivot_longer(captured_fred:captured_scooby, names_to="captured", values_to="valuecaptured") %>%
pivot_longer(unmask_fred:unmask_scooby, names_to="unmask", values_to="valuemask") %>%
pivot_longer(c(caught, captured, unmask), names_to="type", values_to="charactername") %>%
pivot_longer(c(valuecaught, valuecaptured, valuemask)) %>%
# make new column with just character name
separate(charactername, into=c("type2", "charactername"), sep="_") %>%
# count number of actions per character
group_by(type, charactername) %>% count(value) %>% filter(value == "TRUE") %>%
# format the names and actions as sentence case
mutate(charactername = str_to_sentence(charactername),
type = str_to_sentence(type)) %>%
# start plotting
ggplot(aes(x=n, y=charactername, fill=charactername))+
# make a panel per action type
facet_grid(.~type)+
# add segment for lollipop
geom_segment(aes(x = 0, y = reorder(charactername,-n), xend = n, yend = reorder(charactername,-n)),
color = "bisque2", alpha = 0.6, lwd = 2)+
# add point at end of lollipop
geom_point(aes(color=charactername), size = 5, alpha = 0.9)+
# edit color palette
scale_color_manual(values=c("#128a84", "#79af30", "#bb5c37", "#4b0055", "#8e6345"))+
# change global theme
theme_ipsum()+
# edit theme
theme(legend.position = "none", panel.grid.major.y = element_blank(),
strip.text = element_text(hjust = 0.5, face = "bold"))+
# add those labels
labs(x = NULL, y = NULL,
title = "Caught, Captured, Unmasked: \nWho is the best ghost hunter in Mystery Inc?",
subtitle = "Number of times each character was captured, caught the monster, or unmasked the culprit",
caption = "data from Kaggle | plot by @rjstevick for #TidyTuesday")
# Saving -------------------------
ggsave("ScoobyDoo_plot.png", bg = "transparent", width = 9, height = 5, dpi = 400)