generated from jtr13/bookdown-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
03-showcase.Rmd
436 lines (287 loc) · 14.4 KB
/
03-showcase.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# Showcase
Einige Beispiele, was man mit den Funktionen in der Programmiersprache _R_ machen kann.
Code und graphische Darstellungen von Wolf Riepl (https://github.com/fjodor).
https://github.com/fjodor/dataviz_ideas
## ggplot mit emojis
```{r}
# https://github.com/dill/emoGG
# devtools::install_github("dill/emoGG")
library(ggplot2)
library(emoGG)
emoji_search("tulip")
# tulips
ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
geom_emoji(emoji="1f337")
# cars
ggplot(mtcars, aes(wt, mpg))+ geom_emoji(emoji="1f697")
# random
posx <- runif(50, 0, 10)
posy <- runif(50, 0, 10)
ggplot(data.frame(x = posx, y = posy), aes(x, y)) + geom_emoji(emoji="1f63b")
# big emoji in background
qplot(x=Sepal.Length, y=Sepal.Width, data=iris, geom="point") + add_emoji(emoji="1f337")
```
## Datenvorbereitung
We're using data from **Chart2000.com**:<br>Music Charts 2000 - 2020 {data-commentary-width="500"}
```{r setup}
library(knitr)
library(kableExtra)
library(flexdashboard)
library(tidyverse)
library(ggthemes)
library(DT)
library(plotly)
library(EnvStats)
library(ggstatsplot)
library(ggtext)
knitr::opts_chunk$set(echo = FALSE)
all_songs <- read_csv(file = "data/chart2000-songyear-0-3-0062.csv",
na = c("", "-"))
attr(all_songs, "spec") <- NULL
```
```{r setup-theme}
theme_set(theme_solarized(base_size = 15))
theme_update(axis.text.x = element_text(angle = 90),
axis.ticks.x = element_blank(),
panel.grid.major.x = element_blank())
```
```{r data-prep}
top_artists <- all_songs %>%
group_by(artist) %>%
summarise(total_score = sum(indicativerevenue)) %>%
arrange(desc(total_score)) %>%
head(n = 5) %>%
pull(artist)
songs <- all_songs %>%
filter(artist %in% top_artists) %>%
mutate(artist = fct_infreq(artist),
indicativerevenue = round(indicativerevenue))
datatable(songs, filter = "top")
```
From the site [**https://chart2000.com/about.htm\#fairuse**](https://chart2000.com/about.htm#fairuse){.uri} we downloaded the file *chart2000-songyear-0-3-0062*, which you can also find on my github profile at <https://github.com/fjodor/dataviz_ideas>. It contains the top 100 songs for each year from 2000 to 2020.
```{r notes-setup, ref.label = 'setup', eval = FALSE, echo = TRUE}
```
First we filter out the most successful artists: The Top 5 in terms of total score, i. e. the sum of Indicative Revenue (IR). According to <https://chart2000.com/about.htm>, IR is *an attempt to measure the complete revenue generated by a song or album over a certain period. It does take inflation and currency conversion into account and can approximately be related to total revenue generated across the whole music chain in thousands of dollars.*
```{r notes-data-prep, ref.label = 'data-prep', eval = FALSE, echo = TRUE}
```
## Erster Boxplot
Some tweaks to a basic ggplot2 chart<br><br>using **ggthemes** by **Jeffrey B. Arnold** {data-commentary-width="600"}
```{r basic-boxplot}
ggplot(songs, aes(x = artist, y = indicativerevenue)) +
geom_boxplot() +
labs(x = "", y = "Indicative Revenue",
title = "Indicative Revenue by Artist",
subtitle = "Artists sorted by number of songs in Top 100 per year",
caption = "Source: Chart2000.com, Songs of the year, Version 0-3-0062")
```
This is our first attempt at summarizing indicative revenue of the Top 10 artists.
What we have already done:
- Chosen a new theme: **ggthemes::theme_solarized()**. Thanks to package author **Jeffrey B. Arnold** and contributors
- Increased font size
- Ordered artists by total number of songs in top 100 for each year 2000 - 2020
- Rotated x axis labels
- Removed x axis tickmarks
- Removed vertical grid lines
```{r notes-setup-theme, ref.label = 'setup-theme', eval = FALSE, echo = TRUE}
```
```{r notes-basic-boxplot, ref.label = 'basic-boxplot', eval = FALSE, echo = TRUE}
```
## Visuelle Darstellung Ns
by specifying **varwidth = TRUE** {data-commentary-width="600"}
```{r Ns-visually}
ggplot(songs, aes(x = artist, y = indicativerevenue)) +
geom_boxplot(varwidth = TRUE) +
labs(x = "", y = "Indicative Revenue",
title = "Indicative Revenue by Artist",
subtitle = "Artists sorted by number of songs in Top 100 per year",
caption = "Source: Chart2000.com, Songs of the year, Version 0-3-0062")
```
The "trick" here is simply the *varwidth = TRUE* argument in the geom_boxplot() call.
```{r notes-Ns-visually, ref.label = 'Ns-visually', eval = FALSE, echo = TRUE}
```
## Numerische Darstellung Ns
using **EnvStats::stat_n_text()** by **Steven Millard** and **Alexander Kowarik** {data-commentary-width="600"}
```{r Ns-numerically}
ggplot(songs, aes(x = artist, y = indicativerevenue)) +
geom_boxplot(varwidth = TRUE) +
labs(x = "", y = "Indicative Revenue",
title = "Indicative Revenue by Artist",
subtitle = "Artists sorted by number of songs in Top 100 per year",
caption = "Source: Chart2000.com, Songs of the year, Version 0-3-0062") +
stat_n_text(y.pos = 900)
```
We could, of course, calculate Ns and display them using *geom_text()* or *geom_label()*, but we'll use the convencience function *stat_n_text()* from the **EnvStats** package by **Steven Millard** and **Alexander Kowarik** instead.
```{r notes-Ns-numerically, ref.label = 'Ns-numerically', eval = FALSE, echo = TRUE}
```
## Zusammenfassende Statistiken (Mouse-Over)
using **plotly** by **Carson Sievert** {data-commentary-width="600"}
```{r plotly}
songs %>%
plot_ly(x = ~artist, y = ~indicativerevenue,
type = "box")
```
***
Note that **plotly** uses a different calculation method, e. g. a different definition of outliers.
```{r notes-plotly, ref.label = 'plotly', eval = FALSE, echo = TRUE}
```
## Ausreißer markieren
via a **user-defined function** and **geom_text()** {data-commentary-width="600"}
```{r label-outliers}
is_outlier <- function(x) {
return(x < quantile(x, 0.25) - 1.5 * IQR(x) | x > quantile(x, 0.75) + 1.5 * IQR(x))
}
songs %>%
group_by(artist) %>%
mutate(outlier = ifelse(is_outlier(indicativerevenue), song, NA)) %>%
ggplot(aes(x = artist, y = indicativerevenue, color = artist)) +
geom_boxplot(varwidth = TRUE) +
geom_text(aes(label = outlier), na.rm = TRUE, nudge_y = 1500) +
labs(x = "", y = "Indicative Revenue",
title = "Indicative Revenue by Artist",
subtitle = "Artists sorted by number of songs in Top 100 per year",
caption = "Source: Chart2000.com, Songs of the year, Version 0-3-0062") +
scale_color_discrete(guide = NULL)
```
***
A user-defined function (found on [Stackoverflow](https://stackoverflow.com/questions/33524669/labeling-outliers-of-boxplots-in-r), answer by JasonAizkalns) returns songs of outliers, NA otherwise. This newly calculated variable is passed to the **geom_text()** function.
Introducing color for a clearer group distinction. In this case we suppress the color legend, as the groups should be clear from the x axis labels.
```{r notes-label-outliers, ref.label = 'label-outliers', eval = FALSE, echo = TRUE}
```
## Individuelle Datenwerte anzeigen
using **geom_jitter()** {data-commentary-width="600"}
```{r individual-data-points}
ggplot(songs, aes(x = artist, y = indicativerevenue, color = artist)) +
geom_boxplot(varwidth = TRUE, outlier.color = NA) +
geom_jitter(alpha = 0.6, width = 0.2, height = 0) +
labs(x = "", y = "Indicative Revenue",
title = "Indicative Revenue by Artist",
subtitle = "Artists sorted by number of songs in Top 100 per year",
caption = "Source: Chart2000.com, Songs of the year, Version 0-3-0062") +
scale_color_discrete(guide = NULL) +
stat_n_text(y.pos = 900)
```
You can get the same boxplot for different distributions, e. g. a normal distribution (most data points around the mean) vs. a u-shaped distribution (two peaks: one below and one above the mean, with very few data points near the mean).
So displaying all data points gives a better sense of the underlying distributions. It combines macro and micro levels. Inspired by **Edward Tufte**, see his great book *Envisioning Information*.
Note the use of *alpha* (opacity), *width* and *height* in **geom_jitter()**, which reduces overplotting.
```{r notes-individual-data-points, ref.label = 'individual-data-points', eval = FALSE, echo = TRUE}
```
You could use *plotly* here again to display information about songs on hover. At the moment, a hack is needed to remove outliers from the boxplot - maybe support for that will improve in a future version of *plotly*. Plotly's way seems to be to display the data points side-by-side to the boxplot, not overlay it.
## ggplot Aesthetics verwenden
to Include Information on Another Variable<br><br>Combining **shape** and **color**, using **RColorBrewer** by **Erich Neuwirth** {data-commentary-width="600"}
```{r another-variable, fig.width = 8}
songs %>%
rowwise() %>%
mutate(no1 = any(c_across(us:au) == 1, na.rm = TRUE)) %>%
ggplot(aes(x = artist, y = indicativerevenue)) +
geom_boxplot(varwidth = TRUE, outlier.color = NA) +
geom_jitter(alpha = 0.6, width = 0.2, height = 0,
aes(shape = no1, color = no1)) +
labs(x = "", y = "Indicative Revenue",
title = "Indicative Revenue by Artist",
subtitle = "Artists sorted by number of songs in Top 100 per year",
caption = "Source: Chart2000.com, Songs of the year, Version 0-3-0062") +
scale_color_brewer(palette = "Dark2",
name = "No. 1\n(Any Country)?") +
scale_shape_discrete(name = "No. 1\n(Any Country)?") +
stat_n_text(y.pos = 900)
```
***
* Aesthetics for color and shape defined inside **geom_jitter()** (otherwise, there would be separate boxplots for the groups)
* Two aesthetics represented by one legend, as they refer to the same variable **and** are named the same
* Using **RColorBrewer** by **Erich Neuwirth**
```{r notes-another-variable, ref.label = 'another-variable', eval = FALSE, echo = TRUE}
```
## Mittelwerte hinzufügen
for comparison to medians {data-commentary-width="600"}
```{r means}
ggplot(songs, aes(x = artist, y = indicativerevenue, color = artist)) +
geom_boxplot(varwidth = TRUE) +
# geom_jitter(alpha = 0.6, width = 0.2, height = 0) +
stat_summary(fun = "mean", color = "black", shape = 8) +
labs(x = "", y = "Indicative Revenue",
title = "Indicative Revenue by Artist",
subtitle = "Artists sorted by number of songs in Top 100 per year",
caption = "* Mean\n\nSource: Chart2000.com, Songs of the year, Version 0-3-0062") +
scale_color_discrete(guide = NULL) +
stat_n_text(y.pos = 900)
```
***
Interesting to see how in some cases (*The Black Eyed Peas*) median and mean seem to overlap, while in other cases (*Ed Sheeran*) there is a notable gap between median and mean. As is often the case, the mean is considerably higher, as it is influenced by outliers at the high end of the range of values.
Here I'd rather not show the individual data points to avoid information overflow. This chart focuses on summary statistics.
```{r notes-means, ref.label = 'means', echo = TRUE, eval = FALSE}
```
## Statistische Tests anzeigen
for Group Differences<br><br>using **ggstatsplot** by **Indrajeet Patil** {data-commentary-width="600"}
```{r ggstatsplot}
ggstatsplot::ggbetweenstats(
data = songs,
x = artist, xlab = "",
y = indicativerevenue,
ylab = "Indicative Revenue",
plot.type = "box",
type = "p",
conf.level = 0.95,
title = "Indicative Revenue by Artist"
)
```
***
The **ggstatsplot** package by **Indrajeet Patil** is very powerful and flexible, well integrated with *ggplot2* and well documented, see *help(package = "ggstatsplot")*.
Note that this package adds a large number of dependencies.
There are several tests to choose from, including non-parametric, robust and Bayesian.
See *?ggbetweenstats()* and [Website Documentation](https://indrajeetpatil.github.io/ggstatsplot/articles/web_only/ggbetweenstats.html).
```{r notes-ggstatsplot, ref.label = 'ggstatsplot', echo = TRUE, eval = FALSE}
```
## Gruppendifferenzen
Example for a significant test {data-commentary-width="600"}
```{r ggstatsplot2}
songs2 <- all_songs %>%
filter(artist %in% c("Ed Sheeran", "Justin Timberlake", "Miley Cyrus"))
ggstatsplot::ggbetweenstats(
data = songs2,
x = artist, xlab = "",
y = indicativerevenue,
ylab = "Indicative Revenue",
plot.type = "box",
type = "p",
conf.level = 0.95,
title = "Indicative Revenue by Artist"
)
```
***
While **Ed Sheeran** reached the highest average indicative revenue, he also recorded a much higher standard deviation. Therefore, only **Justin Timberlake** averaged significantly higher than **Miley Cyrus** in this comparison. Note the p value correction for multiple comparisons (corrected Holm method).
```{r notes-ggstatsplot2, ref.label = 'ggstatsplot2', echo = TRUE, eval = FALSE}
```
## Bilder als Labels
via **ggtext** by **Claus Wilke** {data-commentary-width="600"}
```{r ggtext-imagelabels, fig.width = 11, fig.height = 7}
# Download and rename files
labels <- c(Rihanna = "<img src='pictures/Rihanna.jpg'
width = '100' /><br>*Rihanna*",
Pink = "<img src='pictures/Pink.jpg'
width = '100' /><br>*Pink*",
'Maroon 5' = "<img src='pictures/Maroon_5.jpg'
width = '100' /><br>*Maroon 5*",
'The Black Eyed Peas' =
"<img src='pictures/Black_Eyed_Peas.jpeg'
width = '100' /><br>*Black Eyed Peas*",
'Ed Sheeran' = "<img src='pictures/Ed_Sheeran.jpg'
width = '100' /><br>*Ed Sheeran*"
)
ggplot(songs, aes(x = artist, y = indicativerevenue, color = artist)) +
geom_boxplot(varwidth = TRUE) +
labs(x = "", y = "Indicative Revenue",
title = "Indicative Revenue by Artist",
subtitle = "Artists sorted by number of songs in Top 100 per year",
caption = "Source: Chart2000.com, Songs of the year, Version 0-3-0062") +
scale_color_discrete(guide = NULL) +
scale_x_discrete(name = NULL, labels = labels) +
stat_n_text(y.pos = 900) +
theme_solarized(base_size = 14) +
theme(axis.text.x = element_markdown(color = "black", angle = 0))
```
***
This is made possible by **Claus Wilke**'s excellent **ggtext** package.
Downloaded the files beforehand and stored them in the report / dashboard folder.
A named vector of labels is passed to *scale_x_discrete()*; besides, *theme(axis.text.x)* needs an *element_markdown()* function.
```{r notes-ggtext-imagelabels, ref.label = 'ggtext-imagelabels', echo = TRUE, eval = FALSE}
```