-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathAvatar.R
120 lines (103 loc) · 5.1 KB
/
Avatar.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
## Tidy Tuesday Week 33 - Avatar: The Last Airbender
## Data Source: appa R package
#Load packages
library(tidyverse)
library(scales)
library(lubridate)
library(extrafont)
library(tvthemes)
#Import data
avatar <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-08-11/avatar.csv')
#Inspect data
View(avatar)
#Wrangle data
a_data <- avatar %>%
mutate("book" = case_when(
book == "Water" ~ "Book One: Water",
book == "Earth" ~ "Book Two: Earth",
TRUE ~ "Book Three: Fire")) %>%
group_by(book, book_num, chapter, chapter_num) %>%
filter(!is.na(imdb_rating)) %>%
summarise("Rating" = mean(imdb_rating))
#Reorder books
a_data$book <- factor(a_data$book) %>%
fct_reorder(a_data$book_num)
a_data$chapter <- factor(a_data$chapter) %>%
fct_reorder(a_data$Rating)
#Calculate mean per book
a_data_mean <- a_data %>%
group_by(book) %>%
summarise_at(vars(Rating),
list("Mean_Rating" = mean))
#Join both datasets together (there's probably a better way of doing this)
a_data_final <- a_data %>%
left_join(a_data_mean)
#Set theme
font_1 <- "Herculanum"
font_2 = "Century Gothic"
background <- "#54544A"
text_colour <- "white"
axis_colour <- "white"
plot_colour <- "#4B4C4E"
theme_style <- theme(text = element_text(family = font_1),
rect = element_rect(fill = background),
plot.background = element_rect(fill = background, color = NA),
plot.title = element_text(family = font_1, face = 'bold', hjust = -2.1, size = 26, colour = text_colour),
plot.subtitle = element_text(family = font_2, size = 15, hjust = -.8, colour = text_colour),
plot.caption = element_text(family = font_2, size = 10, colour = text_colour),
plot.margin = unit(rep(1, 4), "cm"),
panel.background = element_rect(fill = background, color = NA),
panel.border = element_blank(),
panel.grid.major.y = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
axis.title.x = element_text(family = font_2, size = 14, colour= text_colour),
axis.text.x = element_text(family = font_2, size = 10, colour= text_colour),
axis.title.y = element_text(family = font_2, size = 14, colour= text_colour),
axis.text.y = element_text(family = font_2, size = 8, colour= text_colour),
axis.ticks = element_blank(),
axis.line.x = element_line(colour = axis_colour, size = .1),
axis.line.y = element_blank(),
legend.position="none",
strip.text.x = element_text(family = font_1, size = 20, hjust = -.02, colour= text_colour),
strip.placement = "outside",
strip.background = element_blank())
theme_set(theme_classic() + theme_style)
#Set palette
cols <- c("Book One: Water" = "#4EA699",
"Book Two: Earth" = "#AAD922",
"Book Three: Fire" = "#F0803C")
#Set custom y-axis labels
yLabels <- paste(c(0,2.5,5,7.5,10), "")
#Plot data
p1 <- ggplot(a_data_final) +
geom_segment(aes(chapter, xend=chapter, y=0, yend=Rating), size = 0.2, color = "gray") +
geom_hline(aes(yintercept = Mean_Rating), linetype="dashed", colour = text_colour, size = .3) +
geom_point(aes(chapter, y=Rating, colour = book), size=4) +
geom_text(aes(min(a_data_final$chapter_num)+1, vjust = 1, hjust = .01, Mean_Rating,
label = paste0("Average Rating: ",round(Mean_Rating, digits = 1)), family = font_2, size = 4),
colour = "white", size = 5, nudge_y = .3) +
scale_colour_manual(name = "book", values=cols) +
scale_y_continuous(breaks = c(0,2.5,5,7.5,10), limits = c(0, 15), labels = yLabels) +
coord_flip()+
facet_wrap(~book, ncol=1, scale="free_y", strip.position = "top") +
labs(title = "Avatar: The Last Airbender",
subtitle = "IMDb Chapter Ratings Per Book",
caption = "\n\n\nVisualisation: @JaredBraggins | Source: (appa) R Package",
y = 'Rating')
p2 <- ggplot(a_data_final, aes(x = Rating, y = chapter,colour = book)) +
geom_segment(aes(x = Mean_Rating, y = chapter, xend = Rating, yend = chapter)) +
geom_point() +
geom_vline(aes(xintercept = Mean_Rating), linetype="dashed", colour = text_colour, size = .3) +
geom_text(aes(Mean_Rating -0.3, vjust = -28, min(a_data_final$chapter_num),
label = paste0("Average Rating: ",round(Mean_Rating, digits = 1)), family = font_2, size = 5),
colour = text_colour, size = 3, nudge_y = .3) +
scale_colour_manual(name = "book", values=cols) +
facet_wrap(~book, ncol=1, scale="free_y", strip.position = "top") +
labs(title = "Avatar: The Last Airbender",
subtitle = "IMDb Chapter Ratings Per Book",
caption = "\nVisualisation: @JaredBraggins | Source: (appa) R Package",
y = 'Chapter',
x = "Rating")
#Export plot
ggsave("Avatar.png", p2, dpi = 700, width = 25, height = 29, units = "cm")