-
Notifications
You must be signed in to change notification settings - Fork 0
/
ae-11a-data-visualisation.qmd
242 lines (137 loc) · 5.35 KB
/
ae-11a-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
---
title: "Application Exercise 11 - Data Visualisation"
format: html
editor: visual
editor_options:
chunk_output_type: console
execute:
echo: true
message: false
warning: false
---
```{r}
# R Packages that are required for this script are loaded in the beginning
library(readr)
library(dplyr)
library(ggplot2)
# An R package to access the World Bank World Development Indicator timeseries
library(WDI)
# An R package for working with country codes
library(countrycode)
# An R Package to work with model outputs
library(broom)
```
# Data import
## What a waste!
What a Waste is a global project to aggregate data on solid waste management from around the world. This database features the statistics collected through the effort, covering nearly all countries and over 330 cities. Beyond waste generation, there are a long list of additina indicators in this dataset.
- Source: https://datacatalog.worldbank.org/search/dataset/0039597
```{r}
# This code is copied from application exercise week 10
global_waste_data <- read_csv("data/raw_data/country_level_data_0.csv")
```
## World Development Indicators (WDI)
World Development Indicators (WDI) is the primary World Bank collection of development indicators, compiled from officially recognized international sources. It presents the most current and accurate global development data available, and includes national, regional and global estimates.
- World Bank: https://databank.worldbank.org/source/world-development-indicators
- GitHub repo: https://github.com/vincentarelbundock/WDI
- R Package Documentation: http://vincentarelbundock.github.io/WDI/
```{r}
# Only walk through. No live coding.
WDIsearch("GDP per capita ")
gdp_data <- WDI(indicator = "NY.GDP.PCAP.CD", start = 2019, end = 2019)
```
# Data tidying
## Waste data
```{r}
# Only walk through. No live coding.
# This code is copied from application exercise week 10
global_waste_data_small <- global_waste_data %>%
select(country_name,
iso3c,
income_id,
total_msw_total_msw_generated_tons_year,
population_population_number_of_people) %>%
rename(msw_tons_year = total_msw_total_msw_generated_tons_year,
population = population_population_number_of_people)
```
## GDP data
```{r}
# Only walk through. No live coding.
gdp_data_tidy <- gdp_data %>%
as_tibble() %>%
rename(gdp_capita = NY.GDP.PCAP.CD)
```
# Data transformation
## Waste data
```{r}
# This code is copied from application exercise week 10
# Only walk through. No live coding.
global_waste_data_kg_year <- global_waste_data_small %>%
mutate(capita_kg_year = msw_tons_year / population * 1000) %>%
mutate(income_id = factor(income_id,
levels = c("HIC", "UMC", "LMC", "LIC"))) %>%
# select relevant columns
select(country_name:income_id, capita_kg_year)
```
## GDP data
```{r}
# Only walk through. No live coding.
gdp_data_join <- gdp_data_tidy %>%
mutate(iso3c = countrycode(iso2c, origin = "iso2c", destination = "iso3c")) %>%
filter(!is.na(iso3c))
```
## Joining two datasets
```{r}
# Only walk through. No live coding.
waste_gdp_data <- global_waste_data_kg_year %>%
left_join(gdp_data_join) %>%
select(country_name:capita_kg_year, gdp_capita, year)
```
# Data visualisation
## Aesthetic mappings
Main aesthetic mappings:
- x
- y
- color/fill
- shape
- size
- alpha (Transparency)
## Scaling
All functions that adapt the scale start with `scale_` followed by the aesthetic to be adapted, e.g. `x_`, and then followed by the type of variable, e.g. `continouous`. So to adapt a continuous scale on an x-axis, the function to use is: `scale_x_continuous()`
### Axis scales
### Color scales
- scale_color_brewer()
- scale_color_manual()
```{r}
my_colors <- c("#ED6D37", "#901C6C", "#18A5C5", "#A6CE8F")
```
## Geoms
Aesthetic mappings are typically defined within the `ggplot()` function. Everyone geom that follows will use the same aesthetic mappings to visualise the data.
```{r}
```
If we want to define a mapping for one geom only, then the aesthetic mappings must be included in the function of that geom, here `geom_point()`. We can even use additional data in the same plot for an individual geom, see `geom_text()`. - defined for a geom only - geom_text
```{r}
waste_gdp_data_filter <- waste_gdp_data %>%
filter(capita_kg_year > 850 |
country_name == "Switzerland" |
country_name == "Canada")
```
## Labels
## Theme
A reference to @fig-plot-waste-gdp.
```{r}
#| label: fig-plot-waste-gdp
#| fig-cap: "Global Municipal Solid Waste generation over GPD per capita, illustrated by defined income groups."
```
# Data modeling
```{r}
```
Learn more about modeling in R:
- R4DS chapter - https://r4ds.had.co.nz/model-intro.html
- Book: Statistical Inference via Data Science: A ModernDive into R and the Tidyverse! - https://moderndive.com/
- R Packages: Tidymodels - https://www.tidymodels.org/
- R Package broom
https://broom.tidymodels.org/articles/broom.html
# Data communication
Let's add a table of contents a date and an author to the document.
You can learn more about the options for HTML files in the Quarto documentation: https://quarto.org/docs/reference/formats/html.html
And to learn more about creating citeable articles, read here: https://quarto.org/docs/authoring/create-citeable-articles.html