-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path06-wrap-up.Rmd
152 lines (86 loc) · 2.29 KB
/
06-wrap-up.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
---
title: "Wrap up"
output: github_document
---
<https://github.com/maurolepore/open-science-with-r>
**Before you start, collapse all chunks with Alt+o**
## Setup https://www.tidyverse.org/
* Use tidyverse packages.
```{r use-tidyverse-1}
```
```{r use-tidyverse-2}
library(_________)
```
```{r use-tidyverse-3}
library(tidyverse)
```
## Import https://readr.tidyverse.org/
* Read messy data from "data/gap_wide.csv".
```{r import-1}
```
```{r import-2}
messy <- readr::read_csv("_________________")
messy
```
```{r import-3}
messy <- readr::read_csv("data/gap_wide.csv")
messy
```
## Tidy https://tidyr.tidyverse.org/
* Pivot the data on everything except `continent` and `country`.
* Separate the messy column into `metric` and `year`.
```{r pivot-1}
```
```{r pivot-2}
tidy <- messy %>%
pivot_______(cols = -_________:-_______) %>%
# Watch out! year is a character vector, not a number
________(col = name, into = c("______", "____"))
tidy
```
```{r pivot-3}
tidy <- messy %>%
pivot_longer(cols = -continent:-country) %>%
# Watch out! year is a character vector, not a number
separate(col = name, into = c("metric", "year"))
tidy
```
## Transform https://dplyr.tidyverse.org/
* Transform the year column from character to integer
* Filter it to include data since 1980.
* Select all columns except `country`.
```{r verbs-1}
```
```{r verbs-2}
polished <- tidy %>%
______(year = as.integer(____)) %>%
______(year > ____) %>%
______(-_______)
polished
```
```{r verbs-3}
polished <- tidy %>%
mutate(year = as.integer(year)) %>%
filter(year > 1980) %>%
select(-country)
polished
```
## Visualize https://ggplot2.tidyverse.org/
Plot a model for each metric through time:
* Map `color` and `fill` to `continent`.
* Plot model with uncertainty around the mean.
* Facet by `metric` (free the `y` variable -- see `?scales`).
```{r plot-1}
```
```{r plot-2}
______(polished, ___(year, value, fill = _________, color = _________)) +
geom_______() +
______wrap(~ ______, scales = "free_y")
```
```{r plot-3}
ggplot(polished, aes(year, value, fill = continent, color = continent)) +
geom_smooth() +
facet_wrap(~ metric, scales = "free_y")
```
## Communicate https://rmarkdown.rstudio.com/
* Knit to create a report in .pdf format, then in word format.