-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslides.Rmd
More file actions
476 lines (331 loc) · 11.4 KB
/
slides.Rmd
File metadata and controls
476 lines (331 loc) · 11.4 KB
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
---
title: "Visualising Data"
subtitle: "The Good, the Bad, and the Ugly"
author: "Josh Fogg"
output:
xaringan::moon_reader:
lib_dir: libs
css: ["default", "edtheme.scss"]
nature:
highlightStyle: github
highlightLines: true
countIncrementalSlides: false
---
```{r setup, include=FALSE}
options(htmltools.dir.version = FALSE)
library(tidyverse)
library(kableExtra)
```
# The Data Age
Data is **everywhere**. It's part of our news and our politics.
|||
|:-:|:-:|
|BBC Visual and Data Journalism (2019)|Conservative Party Leaflet (2024)|
---
# The Data Age
Data is **everywhere**. It's part of important everyday information.
|||
|:-:|:-:|
|Scottish Power Dashboard|Monzo Bank Statements|
---
# Outline
<br>
### 1. Common visualisation types
### 2. More complex visualisations
### 3. Spotting misleading features
---
class: inverse, middle, center
# 1. Common Visualisations
---
# Data Tables
Data from the 1974 _Motor Trend_ magazine, covering design and performance for 32 cars.
```{r example-table, echo=FALSE}
knitr::kable(tail(mtcars), format = 'html')
```
```txt
mpg: Miles (US) gallon qsec: 1/4 mile time
cyl: Number of cylinders vs: Engine (0 = V-shaped, 1 = straight)
disp: Displacement (cu.in.) am: Transmission (0 = automatic, 1 = manual)
hp: Gross horsepower gear: Number of forward gears
wt: Weight (1000 lbs)
```
---
# Continuous Data
Looking just at `mpg`, which is a number for fuel consumption in miles per gallon.
```{r mpg-table, echo=FALSE}
knitr::kable(head(mtcars[,1, drop=FALSE], 8), format = 'html')
```
---
# Bar Charts
Each bar is the height of the `mpg` data.
```{r bar-chart, echo=FALSE, fig.height=6, fig.width=10, dev='svg'}
ggplot(mtcars) +
geom_bar(stat="identity", aes(x=row.names(mtcars), y=mpg, fill=mpg)) +
theme_minimal() +
labs(
title = "Barplot of MPG by Car Model",
x = "",
y = "miles per gallon",
fill = "Legend"
) +
# flip x axis labels to keep them all nice and readable
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
```
---
# Category Data
Looking just at `cyl`, which is a number of cylinders.
```{r cyl-table, echo=FALSE}
knitr::kable(
list(
mtcars[ 1: 8,2, drop=FALSE],
mtcars[ 9:16,2, drop=FALSE],
mtcars[17:24,2, drop=FALSE],
mtcars[25:32,2, drop=FALSE]
),
format = 'html'
) %>% kable_styling(font_size = 7)
```
This is a 'frequency table' of that data.
```{r cyl-table-freq, echo=FALSE}
cyl_freq <- table(mtcars[,2], dnn=c("Cylinders"))
knitr::kable(cyl_freq, format = 'html') %>%
kable_styling(font_size = 12)
```
---
# Pie Charts
Each slice is the proportion of cars that had that many cylinders.
```{r pie-chart, echo=FALSE, fig.height=6, fig.width=10, dev='svg'}
ggplot(data.frame(cyl_freq), aes(x = "", y = Freq, fill = Cylinders)) +
geom_col() +
geom_text(
aes(label = paste(Freq, " (", round(100*Freq/length(mtcars$cyl)), "%)", sep="")),
position = position_stack(vjust=0.5)
) +
coord_polar(theta = "y") +
labs(title = "Pie Chart of Cylinder Counts") +
theme_void() +
scale_fill_brewer()
```
---
# Doughnut Charts
Each slice is the proportion of cars that had that many cylinders.
```{r doughtnut-chart, echo=FALSE, fig.height=6, fig.width=10, dev='svg'}
hole_size <- 2
cyl_freq <- data.frame(cyl_freq) %>% mutate(x = hole_size)
ggplot(data.frame(cyl_freq), aes(x = hole_size, y = Freq, fill = Cylinders)) +
geom_col() +
geom_text(
aes(label = paste(Freq, " (", round(100*Freq/length(mtcars$cyl)), "%)", sep="")),
position = position_stack(vjust=0.5)
) +
coord_polar(theta = "y") +
xlim(c(0, hole_size + 0.5)) +
labs(title = "Doughtnut Chart of Cylinder Counts") +
theme_void() +
scale_fill_brewer()
```
---
# Beware the Pie Chart
Pie charts are more difficult to compare than bar charts.
```{r bad-pies, out.width=650, out.height=400, echo=FALSE}
knitr::include_graphics("examples/schutz2007.svg")
```
---
# Comparing Continuous Data
Looking at fuel consumption `mpg` and horsepower `hp`.
```{r mpg-hp-table, echo=FALSE}
knitr::kable(head(mtcars[,c(1,4,2), drop=FALSE], 8), format = 'html')
```
---
# Scatter Plot
Two continuous variables can be plotted on a grid, using values to determine location.
```{r scatter-plot, echo=FALSE, fig.height=5.5, fig.width=9.06, dev='svg'}
# plot features that will be common for both
scatter_example <- ggplot(mtcars, aes(x = hp, y = mpg)) +
theme_minimal() +
labs(
title = "Scatter Plot of MPG against Horsepower",
x = "horsepower",
y = "miles per gallon",
) +
ylim(0,35)
# for first example, all point the same
scatter_example + geom_point(size = 4, alpha=0.33, color="blue")
```
---
# Scatter-Factor Plot
We can also use other features like colour and point shape to display categories.
```{r scatter-factor-plot, echo=FALSE, fig.height=5.5, fig.width=10, dev='svg'}
# for second example, points coloured by cylinders
scatter_example +
geom_point(aes(color = factor(cyl)), size = 4, alpha=0.33) +
labs(color="Cylinders") +
scale_color_manual(values = c(
"4" = "red",
"6" = "orange",
"8" = "blue")
)
```
---
# Scatter Trend Plot
We can also identify trend lines between continuous variables.
```{r scatter-trend-plot, echo=FALSE, fig.height=5.5, fig.width=9.06, dev='svg'}
# for second example, points coloured by cylinders
scatter_example +
geom_point(size = 4, alpha=0.33, color="blue") +
geom_smooth(formula=y~I(log10(x)), method = "lm")
```
---
# Continuous Frequencies
Frequency tables for continuous variables aren't very useful.
```{r mpg-freq, echo=FALSE}
cyl_freq <- table(mtcars[,1], dnn=c("mpg"))
knitr::kable(t(head(cyl_freq,20)), format = 'html') %>%
kable_styling(font_size = 14)
```
Instead, we collect the different values into 'bins'.
```{r mpg-bins, echo=FALSE}
mpg_bins <- as.data.frame(table(cut(mtcars$mpg, breaks=seq(5,40, by=5)), dnn="Range"))
knitr::kable(t(mpg_bins), format = 'html')
```
Here `(a,b]` can be read as $$a \lt x \leq b.$$
---
# Histogram
The area of each bar is proportional to the frequency in that 'bin'.
```{r histogram-example, echo=FALSE, fig.height=5.5, fig.width=10, dev='svg'}
ggplot(mtcars, aes(x = mpg)) +
theme_minimal() +
labs(
title = "Histogram of Fuel Efficiency",
x = "miles per gallon",
y = "frequency",
) +
geom_histogram(binwidth = 5, boundary = 0, fill = "#041E42", colour = "white") +
xlim(10, 35) +
ylim(0, 13)
```
---
class: inverse, middle, center
# 2. More Complex Visualisations
---
# Time Series Data
Monthly totals of deaths or serious injuries while driving in Great Britain, 1969 to 1984.
```{r example-timeseries, echo=FALSE}
dfSeatbelts <- data.frame(
# convert time series dates to character format
date = rownames(data.frame(.preformat.ts(Seatbelts), stringsAsFactors = FALSE)),
# drop the three columns we won't use
subset(Seatbelts, select = -c(DriversKilled, PetrolPrice, VanKilled))
)
knitr::kable(head(dfSeatbelts), format = 'html')
```
```txt
date: month and year of recorded data.
drivers: car drivers killed or seriously injured.
front: front-seat passengers killed or seriously injured.
rear: rear-seat passengers killed or seriously injured.
kms: distance driven
law: 0/1: was wearing seatbelts compulsary?
```
---
# Line Graphs
Height of line is the value being tracked, with the date the compulsary seatbelt law came into effect (31 Jan 1983) highlighted.
```{r linegraph-example, echo=FALSE, fig.height=5.5, fig.width=10, dev='svg'}
linegraph <- ggplot(dfSeatbelts) +
theme_minimal() +
labs(
title = "Serious Car Accidents by Month",
x = "Date",
y = "Number",
) +
geom_vline(xintercept=1983, col="orange", alpha=0.4, linewidth=2.5)
linegraph + geom_line(aes(x=as.numeric(time(Seatbelts)), y=drivers), linewidth=1)
```
---
# Multi-Line Graphs
For multiple time series, we can plot each variable as a different line.
```{r multilinegraph-example, echo=FALSE, fig.height=5.5, fig.width=10, dev='svg'}
df <- dfSeatbelts %>%
pivot_longer(cols=c('drivers', 'front', 'rear'),
names_to='Passenger',
values_to='Deaths')
# BUG messed up the dates on this one so had to hack this bit
multi_plot <- ggplot(df, aes(x=1969+time(date)/36.05, y=Deaths)) +
theme_minimal() +
labs(
title = "Serious Car Accidents by Month",
x = "Date",
y = "Number",
) +
scale_color_manual(
name='Passenger Type',
labels=c('Driver', 'Front Seat', 'Back Seat'),
values=c('red', 'purple', 'blue')
) +
scale_fill_manual(
name='Passenger Type',
labels=c('Driver', 'Front Seat', 'Back Seat'),
values=c('red', 'purple', 'blue')
)
multi_plot +
geom_line(aes(color=Passenger)) +
geom_vline(xintercept=1983, col="orange", alpha=0.4, linewidth=2.5)
```
---
# Stacked Area Graphs
Alternatively, we can plot them as areas stacked on top of one another.
```{r stackplot-example, echo=FALSE, fig.height=5.5, fig.width=10, dev='svg'}
multi_plot + geom_area(aes(fill=Passenger), position="stack") +
geom_vline(xintercept=1983, col="gray", linewidth=1)
```
---
class: inverse, middle, center
# Other Visualisation Types
## (the weird ones)
---
# Sankey Diagrams
Used to show how 'units' slow through a system or process.

---
# Sankey Diagrams
More recently, Sankeys have become a popular way to display income and expenditure.
```{r sankey-example, out.width=650, out.height=400, echo=FALSE}
knitr::include_graphics("examples/sankeyart2024.jpg")
```
---
# Treemaps
An alternative to bar or pie charts for more complicated systems
| United Kingdom Product Exports (2019) | Key |
|:-- | :-- |
|  |  |
---
# (Actual) Maps
| Solar Potential (2021) | Swine Flu Cases (2009) | Constituencies (2019) |
| :--------------------- | :--------------------- | :-------------------- |
||||
---
class: inverse, middle, center
# 3. Spotting Misleading Features
---
# Pie Charts
Two typical examples of how pie charts are misused.
| Example 1 | Example 2 |
| :-------: | :-------: |
|  |  |
---
# Bar Charts
Changing the $y$-axis can have a significant impact on bar charts.

---
# Line Graphs
It can also impact line graphs, especially with multiple variables displayed.

---
# Exercise
For the examples we have here consider:
### 1. Which are misleading?
### 2. What might be the impact be?
### 3. How could it be fixed?
---
class: inverse, middle, center
# End