-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatasaurus.Rmd
236 lines (173 loc) · 5.71 KB
/
datasaurus.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
---
title: "datasaurus"
author: "Aurélien Ginolhac"
date: "5/7/2017"
output:
unilur::tutorial_html_solution:
collapse: TRUE
solution_suffix: _exercise
unilur::tutorial_html: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(knitr)
library(kableExtra)
options(knitr.table.format = "html")
```
### datasaurus
- check if you have the package `datasauRus` installed
```{r}
library(datasauRus)
```
- should return nothing. If `there is no package called ‘datasauRus’` appears, it means that the package needs
to be installed. Use this:
```{r, eval=FALSE}
install.packages("datasauRus")
```
### Explore the dataset
Since we are dealing with a `tibble`, we can just type
```{r, eval = FALSE}
datasaurus_dozen
```
only the first **10** rows are displayed.
```{r, echo = FALSE}
datasaurus_dozen %>%
head(10) %>%
kable() %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed"), full_width = FALSE)
```
- what are the dimensions of this dataset? Rows and columns?
```{r, solution = TRUE}
# dim() returns the dimensions of the data frame, i.e number of rows and columns
dim(datasaurus_dozen)
# ncol() only number of columns
ncol(datasaurus_dozen)
# nrow() only number of rows
nrow(datasaurus_dozen)
```
- assign the `datasaurus_dozen` to the `datasaurus_dozen` object. This aims at populating the **Global Environment**
```{r, solution = TRUE}
datasaurus_dozen <- datasaurus_dozen
```
- using Rstudio, those dimensions are now also reported within the interface, where?
```{asis, solution = TRUE}
in the Environment panel -> Global Environment
```
### How many datasets are present?
```{asis, boxtitle = "Tip", box = "lightblue"}
you want to count the number of **unique** elements in the column **dataset**.
The function **length()** returns the length of a vector, such as the unique elements
```
```{r, solution = TRUE}
unique(datasaurus_dozen$dataset) %>% length()
```
### Check summary statistics per dataset
- compute the mean of the `x` & `y` column. For this, you need to `group_by()` the appropriate column and then `summarise()`
```{asis, boxtitle = "Tip", box = "lightblue"}
In `summarise()` you can define as many new columns as you wish. No need to call it for every single variable.
```
```{r, eval = FALSE, solution = TRUE}
datasaurus_dozen %>%
group_by(dataset) %>%
summarise(mean_x = mean(x),
mean_y = mean(y))
```
```{r, echo = FALSE, solution = TRUE}
datasaurus_dozen %>%
group_by(dataset) %>%
summarise(mean_x = mean(x),
mean_y = mean(y)) %>%
kable() %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed"), full_width = FALSE)
```
- compute the standard deviation of the `x` & `y` column in a same way
```{r, eval = FALSE, solution = TRUE}
datasaurus_dozen %>%
group_by(dataset) %>%
summarise(sd_x = sd(x),
sd_y = sd(y))
```
```{r, echo = FALSE, solution = TRUE}
datasaurus_dozen %>%
group_by(dataset) %>%
summarise(sd_x = sd(x),
sd_y = sd(y)) %>%
kable() %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed"), full_width = FALSE)
```
- bonus: do then all in one go using `summarise_if` so we exclude the `dataset` column and compute the others
```{r, eval = FALSE, solution = TRUE}
datasaurus_dozen %>%
group_by(dataset) %>%
summarise_if(is.double, funs(mean = mean, sd = sd))
```
```{r, echo = FALSE, solution = TRUE}
datasaurus_dozen %>%
group_by(dataset) %>%
summarise_if(is.double, funs(mean = mean, sd = sd)) %>%
kable() %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed"), full_width = FALSE)
```
- what can you conclude?
```{asis, solution = TRUE}
all mean and sd are the same for the 13 datasets
```
### Plot the _datasauRus_
- plot the `datasaurus_dozen` with `ggplot` such the **aesthetics** are `aes(x = x, y = y)`
with the **geometry** `geom_point()`
```{asis, boxtitle = "Tip", box = "lightblue"}
the `ggplot()` and `geom_point()` functions must be linked with a **+** sign
```
```{r, fig.height = 8, fig.asp = 1, solution = TRUE}
ggplot(datasaurus_dozen, aes(x = x, y = y)) +
geom_point()
```
- reuse the above command, and now colored by the `dataset` column
```{r, fig.height = 8, fig.asp = 1, solution = TRUE}
ggplot(datasaurus_dozen, aes(x = x, y = y, colour = dataset)) +
geom_point()
```
- expand now by getting one `dataset` per **facet**
```{r, fig.height = 8, fig.asp = 1, solution = TRUE}
ggplot(datasaurus_dozen, aes(x = x, y = y, colour = dataset)) +
geom_point() +
facet_wrap(~ dataset, ncol = 3)
```
- tweak the theme and use the `theme_void` and remove the legend
```{r, fig.height = 8, fig.asp = 1, solution = TRUE}
ggplot(datasaurus_dozen, aes(x = x, y = y, colour = dataset)) +
geom_point() +
theme_void() +
theme(legend.position = "none") +
facet_wrap(~ dataset, ncol = 3)
```
- are the datasets actually that similar?
```{asis, solution = TRUE}
no ;) We were fooled by the summary stats
```
### Animation
- install the `gganimate` package, its dependencies will be automatically installed.
```{r, eval = FALSE, solution = TRUE}
install.packages("gganimate")
```
- add the `dataset` variable to the `frame` argument in the `aes()` function call
```{r, solution = TRUE}
library(gganimate)
p <- ggplot(datasaurus_dozen, aes(x = x, y = y, frame = dataset)) +
geom_point() +
theme_gray(20) +
theme(legend.position = "none")
gganimate(p, title_frame = TRUE, "./img/dino.gif")
```
```{asis, solution = TRUE}

```
### Conclusion
> never trust summary statistics alone; always visualize your data | Alberto Cairo
**Authors**
- Alberto Cairo, (creator)
- Justin Matejka
- George Fitzmaurice
- Lucy McGowan
from this [post](https://itsalocke.com/datasaurus-now-cran/)