-
Notifications
You must be signed in to change notification settings - Fork 0
/
viz_workshop.qmd
486 lines (346 loc) · 15.6 KB
/
viz_workshop.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
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
477
478
479
480
481
482
483
484
485
486
---
title: "Visualizing Data"
---
## Welcome to the Visualizing Data training
This training was originally presented as a workshop at EARNCon 2023.
In this training you will learn how to create data visualizations using free tools like ggplot2 and Tableau.
## 1. Introduction
<!-- speaker notes -->
```{=html}
<!--
-Name
-Pronouns
-role/org
-
-->
```
### 1.1 Brief overview of `ggplot2`
<!-- speaker notes -->
```{=html}
<!--
-`ggplot2` is a visualization library that's highly regarded in the R community because of its flexibility.
-Many of the visualizations seen in scientific journals and professional reports use `ggplot2` because its capable of producing publication-quality graphics with few steps.
-Base R graphics have their own merits, but `ggplot2` stands out because it follows a more structured, iterative way of creating visualizations.
-->
```
`ggplot2` is a data visualization package for R that allows users to create complex plots in a structured manner. It's based on the Grammar of Graphics, which provides a coherent system for describing and building graphics.
### 1.2 Philosophy behind ggplot2
<!-- speaker notes -->
```{=html}
<!--
Fun Fact: The Grammar of Graphics [Book], written by Leland Wilkinson, serves as the foundation for `ggplot2`.
Note: Leland Wilkinson, was VP of Statistics for Tableau before his passing.
Visualizing a plot in `ggplot2` is about understanding its layers. Starting from the data, we layer on aesthetics, and geometries [lines, bars, points, etc.].
This structured layer-wise approach makes constructing and then iterating on charts, clear and intuitive.
-->
```
At the core of `ggplot2` is the idea of layers. This means starting with a blank canvas and then iteratively adding layers to create the desired visualization.
## 2. Basic chart creation
### 2.1. Basic syntax: The Big Three
<!-- speaker notes -->
```{=html}
<!--
- `ggplot2` functions can be broken down intuitively:
- `ggplot()`: Initiates the canvas and data setup.
- `aes()`: Designates which columns of our data map to specific visual properties, such as x and y axes.
- Geometries (e.g., `geom_point()`): Dictate the type of plot, be it a scatter plot, line plot, bar plot, etc.
- Using this approach is like saying: "With this data, using these columns for these aesthetics, render this specific type of plot."
-->
```
1. `ggplot()`
2. `aes()`
3. [geom_point()](https://ggplot2.tidyverse.org/reference/index.html#geoms)
The foundation of any `ggplot2` visualization starts with the `ggplot()` function. Within `ggplot()`, we call `aes()` to designate aesthetic mappings, and then append geometries like `geom_point()` to visually represent data points.
### 2.2. Practical example
<!-- speaker notes -->
```{=html}
<!--
- We'll now delve into the `mtcars` dataset, a default R dataset.
- It's a dataset of cars and their attributes.
- A common relationship to explore is how the horsepower (`hp`) of a car impacts its fuel efficiency, measured as miles per gallon (`mpg`).
- Plotting these variables against each other lets us visually assess their relationship, giving us some sense of covariance.
-->
```
Using the **`mtcars`** dataset, we'll illustrate the relationship between a car's horsepower (**`hp`**) and its fuel efficiency (**`mpg`**).
```{r Load gg and mtcars, message=FALSE}
#First thing first, install Tidyverse using
# install.packages('tidyverse')
# install.packages('highcharter')
#Load tidyverse library
library(tidyverse)
library(here)
mtcars <- mtcars
```
A simple scatter plot:
```{r Car scatter}
# A simple scatter
ggplot(data = mtcars, aes(x=mpg, y=hp)) +
geom_point()
```
A simple histogram using `geom_histogram()`
```{r Histogram}
ggplot(mtcars, aes(x=mpg)) +
geom_histogram(binwidth=3)
```
## 3. Adding layers and customizations
<!-- speaker notes -->
```{=html}
<!--
First thing first, we'll load some pre-tabulated data directly from github page.
It contains variables on employment, union density, and wages by industry. Stuff we commonly work with.
-->
```
```{r Industry data}
# Load some data from Github.
# Major industries, union density, real median wages, and employment. 2000 to 2022
ind_data <- read.csv(url('https://raw.githubusercontent.com/Economic/earn_code_library/main/data/industry_union_wage_emp.csv'))
# ind_data <- read_csv(file = here('data/industry_union_wage_emp.csv'), col_names = TRUE)
#Keep 2022 data
ind_data2022 <- ind_data %>%
filter(year==2022)
ggplot(ind_data2022, aes(x=union_density, y=real_med_wage)) +
geom_point()
```
### 3.1 Adding geometries
<!-- speaker notes -->
```{=html}
<!--
- With `ggplot2`, combining different geometries and aesthetic customizations can really elevate a chart.
- For example, on a scatter geometry, we can transpose "smooth" geometry `geom_smooth()` in the form of a regression line.
--[Run example at this point]
--In this case, adding a secondary geometry gives us a visual diagnostic, that allows us to examine the data for patterns and relationships .
-->
```
An insightful visualization often arises from combining various layers and customizing aesthetics.
Building on our scatter plot from earlier, let's include a smoothed line to better discern the relationship between mpg and hp:
```{r Ind lm plot}
ggplot(data=ind_data2022, aes(x=union_density, y=real_med_wage)) +
geom_point() +
# Add a smoothed line with customized aesthetics
geom_smooth(method = 'lm', se = FALSE, col = "red")
```
The geom_smooth() with method="lm" adds a linear regression line. The se=FALSE ensures the standard error bands are not plotted, and we've chosen a distinct red color for the line.
### 3.2 Customizing Aesthetics
<!-- speaker notes -->
```{=html}
<!--
- In addition to layering geometries, `ggplot2` lets us customize the visual properties of charts fairly extensively.
- Specifically, we can adjust the colors, sizes, shapes, and transparencies of the objects in our plot
-->
```
A major advantage of ggplot2 is its flexibility in customizing visual properties of your plots.
For instance, modifying the scatter plot by adjusting point properties:
```{r Ind aesthetics}
#Bar chart with colors
ggplot(data=ind_data2022, aes(x=mind16, y=real_med_wage, fill=mind16))+
geom_col()
#Bubble scatter chart
ggplot(ind_data2022, aes(x=union_density, y=real_med_wage, size=(total_emp/1000))) +
geom_point()
#Bubble color scatter chart
ggplot(ind_data2022, aes(x=union_density, y=real_med_wage, color=mind16, size=(total_emp/1000))) +
geom_point()
```
Layering and customization in ggplot2 ensures your visualizations are both visually appealing and insightful.
## 4. Customizing Plots with labels and themes
### 4.1. Labeling and Titling
<!-- speaker notes -->
```{=html}
<!--
- Labeling plots is essential part communicating your data. Titles, axis labels, and legends provide the crucial context, and allow your chart to stand alone.
- `ggplot2` can sometimes infer some labels from the data, but it's typically best practice to customize them for clarity and presentation.
-->
```
Labeling is an integral part of making your plots interpretable. While some labels are inferred directly from the data, you often need to specify or customize them.
Here's how to add a title, x-axis label, and y-axis label to our scatter plot with the `labs()` function:
```{r Labeling}
ggplot(data=ind_data, aes(x=year, y=union_density, color=mind16)) +
geom_line() +
# labs() function to add labels
labs(title = 'The share of workers covered by a union contract has steadily decreased... for now',
subtitle = 'Union density by industry, 2000-2022',
x = 'Union density',
y = 'Real median wage',
color= 'Industry',
size = 'Total emp (1000s)',
caption = 'Note: Data refers to workers 16+. Union workers are those who \n are members or covered by a union contract.\nSource: EARN Analysis of CPS ORG data.')
```
### 4.2. Adjusting Text Elements
<!-- speaker notes -->
```{=html}
<!--
- Text elements (like axis labels, titles, and plot text) can be adjusted in terms of size, color, and font.
- Consistent and readable text ensures your plots are easily understandable and can enhance overall aesthetics.
-->
```
Text elements such as titles, axis labels, and annotations can be modified to better fit your plot's aesthetic or to match specific publication requirements.
Here's an example of adjusting the legend's size and position
```{r Text elements}
#Example of our line plot
ggplot(data=ind_data, aes(x=year, y=union_density, color=mind16)) +
geom_line() +
# labs() function to add labels
labs(title = 'The share of workers covered by a union contract has steadily decreased... for now',
subtitle = 'Union density by industry, 2000-2022',
x = 'Union density',
y = 'Real median wage',
color= 'Industry',
size = 'Total emp (1000s)',
caption = 'Note: Data refers to workers 16+. Union workers are those who are members or covered by a union contract.\nSource: EARN Analysis of CPS ORG data.') +
#fix our ugly legend!
theme(legend.position = "bottom",
legend.box = "vertical",
legend.text = element_text(size = 9),
legend.title = element_text(size = 9),
# Edit plot caption
plot.caption = element_text(hjust = 0),
plot.title = element_text(size=12, color='maroon4', face='bold'))
```
### 4.3. Themes in `ggplot2`
<!-- speaker notes -->
```{=html}
<!--
- `ggplot2` lets us easily polish the appearance of plots with themes.
- There are many ready-made themes you can pick from, you can modify existing ones, and you can create your own presets.
- This is especially cool if your organization has a design standard manual, color schemes or logos you regularly use
-->
```
`ggplot2` offers pre-set themes to modify plot aesthetics. Themes are a quick way to change the overall appearance of a plot, ensuring consistency presentations, papers, or reports.
For instance, let's take the line chart we've been working with and apply a black and white theme:
```{r applying themes, fig.height=6, fig.width=10}
#Example of our line plot
ggplot(data=ind_data, aes(x=year, y=union_density, color=mind16)) +
geom_line() +
#Note: Every subsequent theme() will supersede the previous. So be mindful!
theme_bw() +
# theme_dark() +
# theme_light() +
# labs() function to add labels
labs(title = 'The share of workers covered by a union contract has steadily decreased... for now',
subtitle = 'Union density by industry, 2000-2022',
x = 'Union density',
y = 'Real median wage',
color= 'Industry',
size = 'Total emp (1000s)',
caption = 'Note: Data refers to workers 16+. Union workers are those who are members or covered by a union contract.\nSource: EARN Analysis of CPS ORG data.') +
#fix our ugly legend!
theme(legend.position = "bottom",
legend.box = "vertical",
legend.text = element_text(size = 9),
legend.title = element_text(size = 9),
# Edit plot caption
plot.caption = element_text(hjust = 0),
plot.title = element_text(size=12, color='maroon4', face='bold'))
```
Now, let's try using pre-made theme. [ThemePark](https://github.com/MatthewBJane/ThemePark) by Matthew B. Jane
```{r Installing themes}
install.packages("remotes")
remotes::install_github("MatthewBJane/ThemePark")
library(ThemePark)
themepark_themes
```
```{r applying ThemePark themes}
ggplot(data=ind_data, aes(x=year, y=union_density, color=mind16)) +
geom_line() +
#Theme
theme_minimal()+
# labs() function to add labels
labs(title = 'The share of workers covered by a union contract has steadily decreased... for now',
subtitle = 'Union density by industry, 2000-2022',
x = 'Union density',
y = 'Real median wage',
color= 'Industry',
size = 'Total emp (1000s)',
caption = 'Note: Data refers to workers 16+. Union workers are those who are members or covered by a union contract.\nSource: EARN Analysis of CPS ORG data.') +
#fix our ugly legend!
theme(legend.position = "bottom",
legend.box = "vertical",
legend.text = element_text(size = 9),
legend.title = element_text(size = 9),
# Edit plot caption
plot.caption = element_text(hjust = 0, size = 8),
plot.title = element_text(size=12, color='maroon4', face='bold'))+
theme_barbie()
```
\[Insert EARN/EPI/CGI example\]?
Mastering these customization techniques will make your plots informative, engaging, and tailored for their intended audience!
## 5. Exporting your plot
<!-- Speaker notes -->
```{=html}
<!--
- So you've created a beautiful ggplot. Now you want to save the image and tweet/blog/print/frame/put it on the refrigerator
-->
```
Now to export your ggplot for the refrigerator
```{r gg export, , fig.height=6, fig.width=10}
final_plot <- ggplot(data=ind_data, aes(x=year, y=union_density, color=mind16)) +
geom_line() +
theme_minimal() +
# labs() function to add labels
labs(title = 'The share of workers covered by a union contract has steadily decreased... for now',
subtitle = 'Union density by industry, 2000-2022',
x = 'Union density',
y = 'Real median wage',
color= 'Industry',
size = 'Total emp (1000s)',
caption = 'Note: Data refers to workers 16+. Union workers are those who are members or covered by a union contract.\nSource: EARN Analysis of CPS ORG data.') +
#fix our ugly legend!
theme(legend.position = "bottom",
legend.box = "vertical",
legend.text = element_text(size = 9),
legend.title = element_text(size = 9),
# Edit plot caption
plot.caption = element_text(hjust = 0, size = 8),
plot.title = element_text(size=12, color='maroon4', face='bold'))
```
### 5.1 Saving your ggplot
```{r Save options}
# Our chart object
final_plot
ggsave(plot = final_plot,
#name of our chart
filename = 'final_union_industry_scatter.png',
# Save location for our chart
path = here('output/'),
# Dots per inch (300+ is considered high-res)
dpi = 300)
```
## 6. Advanced Plot Types
### 6.1. Faceting and Multi-panel Plots
<!-- speaker notes -->
```{=html}
<!--
- Faceting is a feature that lets you split one plot into multiple plots based on a factor or category.
- Particularly useful when you want to visualize the relationship between variables across different subgroups of your data.
-->
```
Faceting enables the creation of multi-panel plots, helping visualize patterns across different subgroups without generating individual plots for each subgroup.
Let's view scatter plots of mpg vs. hp but facet them by the number of cylinders:
```{r faceting}
ggplot(mtcars, aes(y=hp, x=mpg))+
geom_point()+
facet_wrap(~cyl)
ind_data_years <- ind_data %>%
filter(year %in% c(2000, 2008, 2022))
ggplot(ind_data_years, aes(x=union_density, y=real_med_wage)) +
geom_point() +
facet_wrap(~year) +
theme_bw()
```
### 6.3. Interactive charts
```{r Highcharter, message=FALSE}
#install.packages('highcharter')
library(highcharter)
```
```{r Highcharter line chart}
#Line chart with colors
linechart <- highchart() %>%
hc_add_series(data = ind_data, hcaes(x = year, y = union_density, group = mind16),
type = 'line')
linechart
```
Advanced plot types and features will elevate your data visualization skills, allowing you to craft detailed and insightful plots tailored to diverse datasets and questions.
## 7.0 Resources for layer-based chart visualization!
1. Official Tidyverse page for ggplot2 <https://ggplot2.tidyverse.org/index.html>
2. [R Graphics Cookbook](https://r-graphics.org/)
3. [Highcharter](https://jkunst.com/highcharter/index.html) package for interactive charts