-
Notifications
You must be signed in to change notification settings - Fork 0
/
parfume_tidytuesday_exp.Rmd
404 lines (306 loc) · 11.6 KB
/
parfume_tidytuesday_exp.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
---
title: "TidyTuesday_Dec"
output: html_document
date: "2024-12-09"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r libraries}
library(usethis)
library(tidytuesdayR)
library(tidyverse)
library(gt)
library(gtsummary)
library(ggplot2)
library(janitor)
#library(sf)
library(BAMMtools)
library(magick)
library(camcorder)
library(gifski)
library(quanteda)
library(tidytext)
library(naniar)
library(wordcloud2)
library(classInt)
library(ggbump)
library(gganimate)
```
## Load Data
```{r getdata, echo=FALSE}
pfdattdyr <- tidytuesdayR::tt_load('2024-12-10')
pf_dat1 <-pfdattdyr$parfumo_data_clean %>%
clean_names()
#After data exploration these data are not really clean. Most of the numbers are not unique identifiers nor do perfumes have them
#Secondly, there are 10 records with repeated columnames
pf_dat <- pf_dat1 %>%
filter(brand!='Brand')
```
## Explore missingness
```{r exp1, echo=FALSE}
#full records - but missing numbers
pf_dat %>%
filter(if_all(c(release_year, concentration, rating_value, main_accords, top_notes, middle_notes, base_notes), ~!is.na(.)))
#records to throw out due to not having valuable information
pf_dat %>%
filter(if_all(c(release_year, concentration, rating_value, main_accords, top_notes, middle_notes, base_notes), ~is.na(.)))
gg_miss_var(pf_dat)
```
# Which brands have the most missing data on parfumo?
```{r missinginfo, echo=FALSE}
pf_dat %>%
filter(if_all(c(release_year, concentration, rating_value, main_accords, top_notes, middle_notes, base_notes), ~is.na(.))) %>%
group_by(brand) %>%
add_count() %>%
ungroup() %>%
filter(n>10) %>%
arrange(desc(n)) %>%
select(number, name, brand, url, n)
#The top 25 brands missing data on parfumo
pf_dat %>%
filter(if_all(c(release_year, concentration, rating_value, main_accords, top_notes, middle_notes, base_notes), ~is.na(.))) %>%
count(brand) %>%
arrange(desc(n)) %>%
print(n=25)
topmiss <-pf_dat %>%
filter(if_all(c(release_year, concentration, rating_value, main_accords, top_notes, middle_notes, base_notes), ~is.na(.))) %>%
count(brand) %>%
arrange(desc(n)) %>%
top_n(25) %>%
pull(brand)
```
## Categories of notes
### Floral, herb, spice, fruit(citrus, tree), Spirits(whiskey), wood,
### 'which includes woody, floral, amber, and fresh'
```{r exp2,echo=FALSE}
pf_dat %>%
filter(if_all(c(top_notes, middle_notes, base_notes), ~!is.na(.))) %>%
select(release_year, main_accords, top_notes, middle_notes, base_notes) %>%
filter(if_all(c(main_accords, release_year), ~!is.na(.)))
# Some reading
## https://odeuropa.eu/objectives-timeline/
```
# what is the timeline of the release years?
```{r timeexp, echo=FALSE}
pf_dat %>%
filter(!is.na(main_accords)) %>%
count(release_year) %>%
ggplot(aes(x = release_year, y = n)) +
ylim(0,1750) +
geom_line() +
geom_point() +
labs(title = "Timeline of Year Counts", x = "Year", y = "Count")
#data goes from 1709 to 2024 but 2024 is not 'complete'
```
# Words and their exploration
```{r olfacttextexp, echo=FALSE}
word_counts_accords <- pf_dat %>%
filter(!is.na(main_accords)) %>%
select(main_accords) %>%
tidytext::unnest_tokens(word, main_accords) %>%
count(word, sort = TRUE)
#there are perfumes with notes listed WITHOUT main accords
```
# What are the brand affiliations with different perfumers?
```{r brandperfumer, echo=FALSE}
pf_dat %>%
filter(!is.na(perfumers)) %>%
select(brand, perfumers) %>%
distinct() %>%
arrange(brand)
pf_dat %>%
filter(!is.na(perfumers)) %>%
select(perfumers, brand) %>%
group_by(perfumers, brand) %>%
distinct() %>%
add_count() %>%
ungroup() %>%
arrange(desc(n)) %>%
count(perfumers) %>%
arrange(desc(n))
#Alberto Morillas seems to be most prolific in terms of numbers of perfumes on this list and numbers of brands hes worked with,
#Julien Bedel appears to be second in numbers of perfumes but not in the top 25 for perfumers
#Another way to slice the data is 'Brand owner, Perfume maker' in the Perfumers slot
```
# Making a word cloud of main accords over the years
```{r textmineaccords, echo=FALSE}
#break up the dataset into natural breaks on years
#correctly
# Assuming you want to plot Jenks breaks for the 'dis' variable
pf_dat2 <- pf_dat %>%
filter(!is.na(main_accords) & !is.na(release_year))
#jenks_breaks <- classInt::classIntervals(pf_dat2$release_year, n = 10, style = "jenks")$brks
# Create a new factor variable based on Jenks breaks this is not currently working due to computing
#pf_dat2$year_jenks <- cut(pf_dat2$release_year, breaks = jenks_breaks, include.lowest = TRUE)
pf_dat2$year_brks <- cut(pf_dat2$release_year, breaks = c(1700,1900,1925,1950,1975,1985,1995,2000, 2005, 2010, 2015, 2020, 2025), include.lowest = TRUE)
# Rtutor for the map code
# Create a list of datasets for each model year
release_year_datasets <- pf_dat2 %>%
group_by(year_brks) %>%
group_split()
#Function to create dataframes with ranks of the words
create_worddata <- function(data) {
word_data <- data %>%
select(main_accords) %>%
tidytext::unnest_tokens(word, main_accords) %>%
count(word, sort = TRUE) %>%
arrange(desc(n)) %>%
mutate(rank = seq_len(nrow(.)))
}
wordcounts <-map(release_year_datasets, create_worddata)
# Function to create a word cloud for each dataset
create_wordcloud <- function(data) {
word_data <- data %>%
select(main_accords) %>%
tidytext::unnest_tokens(word, main_accords) %>%
count(word, sort = TRUE)
set.seed(100)
wordcloud2(word_data) + WCtheme(1)
}
# Apply the wordcloud function to each dataset
wordclouds <- map(release_year_datasets, create_wordcloud)
# Display the word clouds
wordclouds
```
#Need to detect the differences between datasets
```{r diff, echo=FALSE}
```
#Using a bump chart for the changes, hopefully 20 is not too much
```{r bumptrialdat, echo=FALSE}
years_label <- data.frame(label = levels(pf_dat2$year_brks), value=1:length(levels(pf_dat2$year_brks)))
wordcounts_df <- bind_rows(wordcounts, .id = 'group_id') %>%
mutate(group_id = as.integer(group_id)) %>%
left_join(., years_label, by=c('group_id'='value')) %>%
mutate(year_brks = factor(as_factor(label), ordered = TRUE)) %>% #forcing an ordered factor to use max and min in plots
select(-c(label, group_id)) %>%
select(year_brks, everything())
```
```{r bumptrial, echo=FALSE, eval=FALSE}
#This is quite interesting but with 20+ colors, this is not easy to tell what it is
ggplot(wordcounts_df, aes(year_brks, rank, group = word, color=word)) +
geom_bump() +
scale_y_reverse()
#let's try labels
ggplot(wordcounts_df, aes(year_brks, rank, group = word, color=word)) +
geom_bump() +
scale_y_reverse() +
geom_text(data = wordcounts_df %>% filter(year_brks == min(year_brks)),
aes(label = word),
hjust = 1,
nudge_x = -0.2) +
geom_text(data = df %>% filter(year_brks == max(year_brks)),
aes(label = word),
hjust = 0,
nudge_x = 0.2) +
theme_minimal() +
theme(legend.position = "none")
#New and improved thanks to rtutor.ai
ggplot(wordcounts_df, aes(year_brks, rank, group = word, color=word)) +
geom_bump() +
scale_y_reverse() +
geom_text(data = wordcounts_df %>% filter(year_brks == min(year_brks)),
aes(label = word),
hjust = 1,
nudge_x = -0.2) +
geom_text(data = wordcounts_df %>% filter(year_brks == max(year_brks)),
aes(label = word),
hjust = 0,
nudge_x = 0.2) +
coord_cartesian(xlim = c(0.5, length(levels(wordcounts_df$year_brks)) + 0.5)) +
theme_minimal() +
theme(legend.position = "none")
#trying with manual shapes
ggplot(wordcounts_df, aes(year_brks, rank, group = word, color=word)) +
geom_bump() +
scale_y_reverse() +
geom_point(aes(shape=word)) +
scale_shape_manual(values=seq(0,22)) +
geom_text(data = wordcounts_df %>% filter(year_brks == min(year_brks)),
aes(label = word),
hjust = 1,
nudge_x = -0.2) +
geom_text(data = wordcounts_df %>% filter(year_brks == max(year_brks)),
aes(label = word),
hjust = 0,
nudge_x = 0.2) +
coord_cartesian(xlim = c(0.5, length(levels(wordcounts_df$year_brks)) + 0.5)) +
theme_minimal() +
theme(legend.position = "none")
```
#could possibly also animate the rankings of the words across the year breaks using gganimate
```{r animatemainaccords, echo=FALSE, eval=FALSE}
'! time data must either be an integer, numeric, POSIXct, Date, difftime, or hms object'
#I think to get geom_bump to work with gganimate need to convert to a time period and then use transition events or transition time
bumpp <-ggplot(wordcounts_df, aes(year_brks, rank, group = word, color=word)) +
geom_bump() +
scale_y_reverse() +
geom_point(aes(shape=word)) +
scale_shape_manual(values=seq(0,22)) +
geom_text(data = wordcounts_df %>% filter(year_brks == min(year_brks)),
aes(label = word),
hjust = 1,
nudge_x = -0.2) +
geom_text(data = wordcounts_df %>% filter(year_brks == max(year_brks)),
aes(label = word),
hjust = 0,
nudge_x = 0.2) +
coord_cartesian(xlim = c(0.5, length(levels(wordcounts_df$year_brks)) + 0.5)) +
theme_minimal() +
theme(legend.position = "none") +
transition_reveal(year_brks, transition_length = 2, state_length = 1, wrap=FALSE)
enter_fade() +
exit_fade()
# Render the animation
animate(bumpp, nframes = 100, fps = 10)
```
#Building the animation from scratch
```{r animationscratch, echo=FALSE}
brks <- levels(wordcounts_df$year_brks)
#takes a list from a vector and starts there
create_dfs <- function(data, value) {
df <-data %>%
mutate(word = ifelse(year_brks>value, NA, word),
rank = ifelse(year_brks>value, NA, rank))
return(df)
}
vec_int <- brks[2:length(brks)] # taking the second value of the vector and running the datasets to the end
seq_dfs <-map(vec_int, ~create_dfs(wordcounts_df, .x))
makeplots <- function(df) {
ggplot(df, aes(year_brks, rank, group = word, color=word)) +
geom_bump() +
scale_y_reverse(breaks = seq(1,21, by=1)) +
geom_point(aes(shape=word)) +
scale_shape_manual(values=seq(0,22)) +
geom_text(data = df %>% filter(year_brks == min(year_brks)),
aes(label = word),
hjust = 1,
nudge_x = -0.2) +
geom_text(data = df %>% filter(!is.na(word)) %>% filter(year_brks==max(year_brks)),
aes(label = word),
hjust = 0,
nudge_x = 0.2) +
coord_cartesian(xlim = c(0.5, length(levels(wordcounts_df$year_brks)) + 0.5)) + #not the best function line it calls external dataset
theme_minimal() +
theme(legend.position = "none") +
labs(
title = "Rank of Main Accords for Parfumes by Release Year",
subtitle = "Often Floral and Spicy, recently, Sweet",
caption = "Each parfume may have multiple main accords noted \n Data source:Parfumo via https://www.kaggle.com/olgagmiufana1",
x = 'Year of Release (binned)'
)
}
plotspack <- map(seq_dfs, makeplots)
```
```{r makegif, echo=FALSE}
gg_record(
dir = file.path("/cloud/project", 'recording2'),
device = 'png',
width = 12,
height = 6
)
plotspack
gg_stop_recording()
png_files <- list.files("/cloud/project/recording2", pattern = ".*png$", full.names = TRUE)
gifski(png_files, gif_file = "pf_plot.gif", width = 1200, height = 600, delay = 2)
```