-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathae-11b-data-visualisation.qmd
331 lines (183 loc) · 9.68 KB
/
ae-11b-data-visualisation.qmd
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
---
title: "Programming Exercise - Data Visualisation"
format: html
editor: visual
bibliography: references.bib
execute:
echo: true
message: false
warning: false
editor_options:
chunk_output_type: console
---
# Task 0: Load R packages
1. Add a new code-chunk below the three outlined steps for Task 0
2. Load the `gapminder`, `ggplot2` and `dplyr` R Packages
3. Run the code contained in the code-chunk and fix any errors
# Task 1: Data import
We are not importing any external data, we will use data from the `gapminder` R Package. The step of "Data import" is listed to remind us of this step in the data science lifecycle.
# Task 2: Data tidying
The data is tidy and we can directly work with it. The step is here as reminder.
# Task 3: Data transformation
## Data for 2007
**Fill in the gaps**
1. A code-chunk has already been created
2. Start with the `gapminder` object
3. Add the pipe operator (`%>%`) and on a new line use the `filter()` function to narrow down the data for observation of the year 2007
4. Use the assignment operator (`<-`) to assign the data to an object named `gapminder_yr_2007`
5. Run the code contained in the code-chunk and fix any errors
6. Next to the code-chunk option `#| eval:` change the value from `false` to `true`
7. Render
8. Add, Commit, Push
```{r}
#| eval: false
# A subset only for 2007
___ <- ___ %>%
___(___ == ___)
```
## Median life expectancy and country frequency by continent
**Fill in the gaps**
1. A code-chunk has already been created
2. Start with the `gapminder_yr_2007` object
3. Add the pipe operator (`%>%`) and on a new line use the `group_by()` function to group the following operations by the variable continent.
4. Add the pipe operator (`%>%`) and on a new line use the `summarise()` function to summarise the data for the count and the median life expectancy.
5. Use the assignment operator (`<-`) to assign the data to an object named `gapminder_summary_continent_2007`
6. Run the code contained in the code-chunk and fix any errors
7. Next to the code-chunk option `#| eval:` change the value from `false` to `true`
8. Render
9. Add, Commit, Push
```{r}
#| eval: false
# A subset for median life expectancy in 2007 by continent
gapminder_summary_continent_2007 <- gapminder_yr_2007 %>%
___(___) %>%
___(
count = n(),
lifeExp = median(___)
)
```
## Median life expectancy by continent and year
**Fill in the gaps**
1. A code-chunk has already been created
2. Start with the `gapminder` object
3. Add the pipe operator (`%>%`) and on a new line use the `group_by()` function to group the following operations by the variables continent and year.
4. Add the pipe operator (`%>%`) and on a new line use the `summarise()` function to summarise the data for the median life expectancy and name the new colum 'lifeExp'.
5. Use the assignment operator (`<-`) to assign the data to an object named `gapminder_summary_continent_year`
6. Run the code contained in the code-chunk and fix any errors
7. Next to the code-chunk option `#| eval:` change the value from `false` to `true`
8. Render
9. Add, Commit, Push
```{r}
#| eval: false
# A subest for median life expectancy by continent and year
gapminder_summary_continent_year <- gapminder %>%
___(continent, ___) %>%
summarise(___ = ___(___))
```
# Task 4: Data visualisation
## Boxplot
1. Add a new code-chunk below
2. Use the `ggplot()` function and the `gapminder_yr_2007` object to create a boxplot with the following aesthetic mappings:
- continent to the x-axis;
- life expactancy to the y-axis;
- continent to color using the `fill = continent` argument inside `aes()`
3. Do not display (ignore) the outliers in the plot.
4. Run the code contained in the code-chunk and fix any errors
## Timeseries plot
1. Add a new code-chunk below
2. Use the `ggplot()` function and the `gapminder_summary_continent_year` object to create a connected scatterplot (also timeseries plot) using the `geom_line()` and `geom_point()`functions with the following aesthetic mappings:
- year to the x-axis;
- life expactancy to the y-axis;
- continent to color using the `color = continent` argument inside `aes()`
3. Run the code contained in the code-chunk and fix any errors
## Barplot
### with `geom_col()`
1. Add a new code-chunk below
2. Use the `ggplot()` function and the `gapminder_summary_continent_2007` object to create a barplot using the `geom_col()` function with the following aesthetic mappings:
- continent to the x-axis;
- count to the y-axis;
3. Run the code contained in the code-chunk and fix any errors
### with `geom_bar()`
1. Add a new code-chunk below
2. Use the `ggplot()` function and the `gapminder_yr_2007` object to create a barplot using the `geom_bar()` function with the following aesthetic mappings:
- continent to the x-axis;
3. Run the code contained in the code-chunk and fix any errors
4. The plot is identical to the plot created with `geom_col()`. Why? What does the `geom_bar()` function do? Write your text here:
## Histogram
1. Add a new code-chunk below
2. Use the `ggplot()` function and the `gapminder_yr_2007` object to create a histogram using the `geom_histogram()` function with the following aesthetic mappings:
- life expactancy to the x-axis;
- continent to color using the `fill = continent` argument inside `aes()`
3. Run the code contained in the code-chunk and fix any errors.
4. Inside the `geom_histogram()` function, add the following arguments and values:
- `col = "grey30"`
- `breaks = seq(40, 85, 2.5)`
5. Run the code contained in the code-chunk and fix any errors.
6. Describe how the `geom_histogram()` function is similar to the `geom_bar()` function.
7. What happens by adding the 'breaks' argument? Play around with the numbers inside of `seq()` to see what changes.
## Scatterplot and faceting
1. Use the `ggplot()` function and assign `gapminder_yr_2007` to the data argument and create a scatterplot using the `geom_point()`function with the following aesthetic mappings:
- gdpPercap the x-axis;
- lifeExp to the y-axis;
- population to the size argument;
- country to color using the `color = continent` argument inside `aes()`
2. Run the code contained in the code-chunk and fix any errors.
3. Use the variable continent to facet the plot by adding: `facet_wrap(~continent)`
4. Run the code contained in the code-chunk and fix any errors
5. Describe in your own words what the `facet_wrap()`function does
6. Adapt the scaling of the size by adding `scale_size(range = c(2, 12))` on the next line
7. Run the code contained in the code-chunk and fix any errors.
8. Adapt the scaling of the x-axis to log10 by adding `scale_x_log10(labels = scales::label_log(), limits = c(100, 100000))` on the next line
9. Run the code contained in the code-chunk and fix any errors
## Lineplot and faceting
1. A code-chunk with complete code has already been prepared
2. Run the code contained in the code-chunk and fix any errors
3. Remove \# sign at the line that starts with the `scale_color_manual()` function
4. What is stored in the `country_colors` object? Find out by executing the object in the Console (type it to the Console and hit enter). Do the same again, but with a question mark `?country`.
5. Next to the code-chunk option `#| eval:` change the value from `false` to `true`
6. Render
```{r}
#| eval: false
ggplot(data = gapminder,
mapping = aes(x = year,
y = lifeExp,
group = country,
color = country)) +
geom_line(lwd = 1, show.legend = FALSE) +
facet_wrap(~continent) +
# scale_color_manual(values = country_colors) +
theme_minimal()
```
## Choropleth Maps
You can also prepare maps with `ggplot2`. It's beyond the scope of the class to teach you the foundations of spatial data in R, but a popular package to work with spatial data is the `sf` (Simple Feautures) R Package. The `rnaturalearth` R Package facilitates world mapping by making [Natural Earth](https://www.naturalearthdata.com/) map data more easily available to R users.
The code-chunk below contains code that for a world map that shows countries by income group. If you want to view that map, do the following:
1. Install the `sf` and the `rnaturalearth` R Packages.
2. Load the R Packages at the beginning of this file alongside the other loaded R Packages.
3. Run the code contained in the code-chunk and fix any errors
4. Next to the code-chunk option `#| eval:` change the value from `false` to `true`
5. Render
6. Add, Commit, Push
```{r}
#| eval: false
world <- ne_countries(scale = "small", returnclass = "sf")
world %>%
mutate(income_grp = factor(income_grp, ordered = T)) %>%
ggplot() +
geom_sf(aes(fill = income_grp)) +
theme_void() +
theme(legend.position = "top") +
labs(fill = "Income Group:") +
guides(fill = guide_legend(nrow = 2, byrow = TRUE))
```
The code for the code-chunk is taken from here: More here: https://bookdown.org/alhdzsz/data_viz_ir/maps.html
## Working with spatial data in R
If you are interested in working with spatial data in R, then I recommend the following resources:
- Geocompuation with R - Book: https://geocompr.robinlovelace.net/
- Simple Features for R - Article: https://r-spatial.github.io/sf/articles/sf1.html
- tmap: thematic maps in R - R Package: https://r-tmap.github.io/tmap/
# Task 5: Data communication
1. Use the Quarto Documentation Guide for HTML format to figure out how to add a theme to this Quarto document.
https://quarto.org/docs/guide/
2. Apply a theme to this Quarto document.
# Task 6: Complete assignment
Open an issue on the repo for this exercise to let us know you completed it. Use the @larnsce mention.