-
Notifications
You must be signed in to change notification settings - Fork 0
/
coffee_functions.R
313 lines (286 loc) · 9.63 KB
/
coffee_functions.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
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
coffee_colors <- tibble::tribble(
~coffee_type, ~color_code,
"Coffee A", "#E41A1C",
"Coffee B", "#377EB8",
"Coffee C", "#4DAF4A",
"Coffee D", "#984EA3"
)
coffee_pal <- coffee_colors$color_code
names(coffee_pal) <- coffee_colors$coffee_type
# tmaptools::get_brewer_pal("Set1", n = 10) |> list()
match_colors = tibble::tribble(~ids, ~color2,
"A D", "#FFB716",
"A ", "#E41A1C", # A
"A CD", "#9ecae1",
"ABC ", "#FF7F00", #
" B ", "#377EB8", # B
" C ", "#4DAF4A", # C
" D", "#984EA3", # D
"ABCD" ,"#808080",
" BC ", "#A65628",
NA, "#e5e5e5")
flavor_colors = tibble::tribble(~ids, ~color,
"Fruity", "#ee3087",
"Sour/Acid", "#e6550d", #
"Alcohol/Fermented", "#9ecae1",
"Green/Vegetative", "#3fa746", #
"Stale/Papery", "#fee6ce", #
"Earthy", "#ff3d19", ##
"Chemical", "#9ecae1", ###
"Roasted" , "#747e7d",
"Cereal", "#eedaac",
"Spices", "#ff6a6d",
"Nutty", "#d3a778",
"Cocoa" , "#3b281e", # chocolate?
"Sweet" , "#f2f4f4",
"Floral", "#abb9dc")
gg_bars <-
function(df,
x_axis,
y_axis,
fill = NULL, # what to fill by
y_label_type = scales::percent,
bar_label_type = scales::percent_format(accuracy = 1),
single_color = FALSE, # If bars are one color or follow a palette
bar_color = NULL, # single_color must be TRUE
base_size = 12) {
max_lim = max(df[[{{ y_axis }}]], na.rm = TRUE) * 1.1
if (single_color == FALSE) {
p <- ggplot(df, aes(!!sym(x_axis), !!sym(y_axis), fill = !!sym(fill))) +
geom_col() +
geom_text(
aes(label = bar_label_type(!!sym(y_axis)), color = !!sym(fill)),
fontface = "bold",
vjust = -0.4,
size = base_size * (4/14)
)
}
else{
p <- ggplot(df, aes(!!sym(x_axis), !!sym(y_axis))) +
geom_col(fill = bar_color) +
geom_text(
aes(label = bar_label_type(!!sym(y_axis))),
color = bar_color,
fontface = "bold",
vjust = -0.4,
size = base_size * (4/14)
)
}
return(
p + theme_coffee(base_size) +
scale_y_continuous(
expand = c(0, 0),
limits = c(0, max_lim),
labels = y_label_type
)
)
}
# Used to save wordcloud
save_widget <- function(wd) {
htmlwidgets::saveWidget(
widget = wd,
file = paste0("assets/widgets/",deparse(substitute(wd)), ".html"),
selfcontained = TRUE
)
}
gt_add_coffee_color <- function(gt, coff_col){
gt |> gt::text_transform(
locations = cells_body(
columns = !!sym(coff_col)
),
fn = function(x){
color = filter(coffee_colors, coffee_type == x)$color_code
name <- x
# team <- word(x, -1)
glue::glue(
"<div><span style='font-weight:bold;color:{color}'>{name}</div>"
)
})
}
find_two_words <- function(str, two_word){
c = stringi::stri_count_regex(str, pattern = two_word)
rep(two_word, c)
}
count_cupping_notes <-
function(cup_list,
count_min = 20) {
remove_words = c("nice", "and", "aftertaste", "better", "bit", "but", "drink", "favorite", "feel", "felt",
"finish", "first", "for", "good", "got", "too", "out", "that", "than", "the", "thin", "this",
"very", "one", "had", "when", "more", "high", "little", "was", "would", "some", "not", "would",
"with", "really", "flavor", "mouth", "notes", "taste", "less", "most", "well", "diner", "after",
"much", "tasted", "like", "almost", "tastes", "cup", "flavors", "slightly", "slight", "liked",
"just", "tasting", "note", "notes", "also", "what", "maybe", "from", "similar", "its", "didnt",
"could", "all", "coffee", "coffees", "which", "probably", "something", "way", "dont", "did",
"lot", "over", "have", "process", "time", "down", "brown", "green", "mouthfeel", "body", "cool",
"cools", "cooled")
# The first part of each regex will be used to count later on (i.e. black tea)
two_words = c(
"brown sugar",
"black tea|tea like|tea-like|tealike",
"olive oil|olives",
"dark chocolate|dark choc|dark chocolatey",
"green apple|green apples",
"full body|high body|heavy body|heavier body|fuller body|full bodied|full-bodied",
"medium body|medium bodied",
"light body|lighter body|little body|low body|light bodied"
)
# find_two_words(word, "brown sugar")
# two_words_counts = purrr::map(two_words, find_two_words, str = word) |> unlist()
# two_words_counts = sub("\\|.*", "", two_words_counts)
# Put all words in a single long string
cup_words = cup_list |> stringr::str_to_lower() |>
paste0(collapse = " ")
two_words_counts = purrr::map(two_words, find_two_words, str = cup_words) |> unlist()
two_words_counts = sub("\\|.*", "", two_words_counts)
# Also remove dark chocolate bc we will count chocolate
cup_words = stringi::stri_replace_all(str = cup_words, regex = "dark chocolate", replacement = "")
cup_words = cup_words |> stringr::str_split(pattern = " |,|\n|/") |> unlist()
cup_words = stringi::stri_replace_all_regex(
cup_words,
pattern = c(
"[[:punct:]]|[0-9]+",
"raspberries",
"strawberries",
"blueberries",
"^berries$",
"^cherries",
"^fruit$|^fruit$|fruitful|fruitiest|fruitiness|fruitier|fruityness|fruits$|fruite$|fruty$",
"chocolatey",
"acidic|acidity",
"bitterness",
"citrusy",
"^smokey$|^smoke$",
"^wood$",
"^balance$",
"^roast$|roasty",
"^toast$|toasty",
"^nuts$|^nut$",
"^darker$",
"sweetness",
# "^cool$|^cools$",
"fermenty",
"^funk$",
"^love$",
"juice$",
"tealike",
"^apples$",
"^stone",
"^wine$",
"^cloves$",
"paper",
"^lemony$|lemonade",
"herby|herbed|herb|herbs", # replace dark chocolate with nothing
"peanuts",
"hazelnuts",
"grainy|grains",
"malty",
"vegetable|vegetables|vegetal|vegetabley",
"raisins",
"bananas"
),
replacement = c(
"",
"raspberry",
"strawberry",
"blueberry",
"berry",
"cherry",
"fruity",
"chocolate",
"acid",
"bitter",
"citrus",
"smoky",
"woody",
"balanced",
"roasted",
"toasted",
"nutty",
"dark",
"sweet",
# "cooled",
"fermented",
"funky",
"loved",
"juicy",
"tea",
"apple",
"stone fruit",
"winey",
"clove",
"papery",
"lemon",
"herb-like",
"peanut",
"hazelnut",
"grain",
"malt",
"vegetative",
"raisin",
"banana"
),
vectorize_all = FALSE
)
cup_words = cup_words[!cup_words %in% remove_words]
cup_words = cup_words[nchar(cup_words) > 2] # Above removes punctuation
# Add the two words counts
cup_words = c(cup_words, two_words_counts)
cup_words_table = cup_words |> table()
more_than_x = cup_words_table[cup_words_table > count_min]
return(more_than_x[order(more_than_x, decreasing = TRUE)])
}
prepare_words <- function(coffee_words, color = "#000") {
coffee_df = data.frame(coffee_words) |> mutate(cup_words = stringr::str_to_title(cup_words),
Freq = color)
coffee_df = coffee_df |> mutate(
cup_words = recode(
cup_words,
"Acid" = "Sour/Acid",
"Caramel" = "Caramelized",
"Citrus" = "Citrus Fruit",
Vegetal = "Vegetative",
"Papery" = "Stale/Papery",
"Apple" = "Green Apple"
)
)
return(coffee_df)
}
coffee_sunburst <- function(coffee_cup_df, mcolor = ~color, hover = ~new_label) {
# coffee_cup_df <-
# coffee_cup_df |> mutate(new_label = paste0(
# "<b>",
# end_name,
# ": ",
# "</b>",
# ifelse(is.na(labels), "", stringr::str_wrap(labels, width = 30))
# ))
coffee_cup_df |> plot_ly() |> add_trace(
marker = list(colors = mcolor),
type = 'sunburst',
ids = ~ ids,
labels = ~ end_name,
parents = ~ parents,
text = hover,
textinfo = 'label',
# What shows on the chart
hovertemplate = '%{text}<extra></extra>',
domain = list(column = 1),
maxdepth = 3,
insidetextorientation = 'radial'
) |>
layout(margin = list(
l = 0,
r = 0,
b = 0,
t = 0
)) |>
as_widget()
}
coffee_words_join <-
function(coffee_words,
coffee_cup_df,
color = "#fee6ce") {
coffee_join = left_join(coffee_cup_df, coffee_words, by = c("end_name" = "cup_words"))
coffee_join = coffee_join |> mutate(color = ifelse(is.na(Freq), color, Freq))
return(coffee_join)
}