forked from dgrtwo/data-screencasts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bob-ross.Rmd
193 lines (155 loc) · 4.59 KB
/
bob-ross.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
---
title: "Untitled"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library(tidyverse)
theme_set(theme_light())
bob_ross <- readr::read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-08-06/bob-ross.csv")
```
```{r}
bob_ross_gathered <- bob_ross %>%
janitor::clean_names() %>%
gather(element, present, -episode, -title) %>%
filter(present == 1) %>%
mutate(title = str_to_title(str_remove_all(title, '"')),
element = str_to_title(str_replace(element, "_", " "))) %>%
select(-present) %>%
extract(episode, c("season", "episode_number"), "S(.*)E(.*)", convert = TRUE, remove = FALSE) %>%
arrange(season, episode_number)
```
### Exploring the paintings
```{r}
bob_ross_gathered %>%
count(element, sort = TRUE) %>%
head(25) %>%
mutate(element = fct_reorder(element, n)) %>%
ggplot(aes(element, n)) +
geom_col() +
coord_flip()
```
What are the most "crowded" paintings, with the most elements in them?
```{r}
bob_ross_gathered %>%
add_count(episode) %>%
arrange(desc(n))
```
How have Ross's paintings been changing over time?
```{r}
by_season_element <- bob_ross_gathered %>%
filter(!element %in% c("Tree", "Trees")) %>%
group_by(season) %>%
mutate(number_episodes = n_distinct(episode)) %>%
count(season, element, number_episodes, sort = TRUE) %>%
mutate(percent_included = n / number_episodes) %>%
group_by(element) %>%
mutate(element_total = sum(n)) %>%
ungroup()
by_season_element %>%
filter(element_total >= 50) %>%
ggplot(aes(season, percent_included, color = element)) +
geom_line() +
scale_y_continuous(labels = scales::percent_format()) +
expand_limits(y = 0) +
facet_wrap(~ element)
```
Could have used: [many models with broom](https://r4ds.had.co.nz/many-models.html)
### Clustering
What tends to appear together?
```{r}
library(widyr)
correlations <- bob_ross_gathered %>%
add_count(element) %>%
filter(n >= 5) %>%
pairwise_cor(element, episode, sort = TRUE)
correlations %>%
filter(item1 == "River") %>%
mutate(item2 = fct_reorder(item2, correlation)) %>%
ggplot(aes(item2, correlation)) +
geom_col() +
coord_flip() +
labs(title = "What tends to appear with a river?",
subtitle = "Among elements that appeared in at least 10 paintings")
correlations %>%
filter(item1 == "Snow") %>%
mutate(item2 = fct_reorder(item2, correlation)) %>%
ggplot(aes(item2, correlation)) +
geom_col() +
coord_flip() +
labs(title = "What tends to appear with snow?",
subtitle = "Among elements that appeared in at least 10 paintings")
```
```{r}
library(ggraph)
library(igraph)
set.seed(2019)
correlations %>%
head(100) %>%
graph_from_data_frame() %>%
ggraph() +
geom_edge_link(aes(alpha = correlation)) +
geom_node_point() +
geom_node_text(aes(label = name), vjust = 1, hjust = 1) +
theme_void()
```
### Principal Component Analysis
What dimensions drive a lot of the variation among paintings?
```{r}
library(reshape2)
library(broom)
library(tidytext)
binary_matrix <- bob_ross_gathered %>%
acast(title ~ element)
# Center the columns
centered_matrix <- t(t(binary_matrix) - colMeans(binary_matrix))
svd_result <- svd(centered_matrix)
element_weights <- tidy(svd_result, matrix = "v") %>%
mutate(element = colnames(binary_matrix)[column])
element_weights %>%
filter(PC <= 4) %>%
group_by(PC) %>%
top_n(16, abs(value)) %>%
ungroup() %>%
mutate(element = reorder_within(element, value, PC)) %>%
ggplot(aes(element, value, fill = factor(PC))) +
geom_col(show.legend = FALSE) +
facet_wrap(~ PC, scales = "free") +
scale_x_reordered() +
coord_flip() +
labs(title = "First four principal components of elements in Bob Ross paintings")
```
1. Mountains/Conifer vs Ocean/Beach and deciduous trees
2. Trees, especially deciduous, vs Ocean
3. Spring/Summer vs Winter
4. Lake vs River
```{r}
painting_weights <- broom::tidy(svd_result, matrix = "u") %>%
mutate(painting = rownames(binary_matrix)[row])
```
```{r}
painting_weights %>%
filter(PC == 1) %>%
arrange((value))
bob_ross_gathered %>%
filter(title == "Frozen Solitude")
painting_weights %>%
filter(PC <= 4) %>%
group_by(PC) %>%
top_n(20, abs(value)) %>%
ungroup() %>%
mutate(painting = reorder_within(painting, value, PC)) %>%
ggplot(aes(painting, value, fill = factor(PC))) +
geom_col(show.legend = FALSE) +
facet_wrap(~ PC, scales = "free") +
scale_x_reordered() +
coord_flip() +
labs(title = "First four principal components of Bob Ross paintings")
```
```{r}
broom::tidy(svd_result, matrix = "d") %>%
ggplot(aes(PC, percent)) +
geom_point()
```