-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathggplot_flipbook_xaringan.Rmd
189 lines (132 loc) · 7.84 KB
/
ggplot_flipbook_xaringan.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
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
---
title: "the ggplot flipbook"
subtitle: "made with xaringan"
author: "Gina Reynolds"
date: "2019/01/31"
output:
xaringan::moon_reader:
chakra: libs/remark-latest.min.js
lib_dir: libs
css: ["kunoichi", "ninjutsu"]
nature:
ratio: "20:10"
highlightLines: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, warning = F, message = F)
knitr::opts_chunk$set(fig.height = 6, out.width = "100%", comment = " ", cache = F, dpi = 300)
source("https://raw.githubusercontent.com/EvaMaeRey/little_flipbooks_library/master/xaringan_reveal_parentheses_balanced.R")
```
<!-- #Table of Contents -->
<!-- - [Visual Table of Contents](#visualtoc) -->
<!-- - [Introduction](#introduction) -->
<!-- - [What's the slow ggplot workflow?](#whatsslow) -->
<!-- - [Getting started with R and RStudio](#gettingstarted) -->
<!-- - [Acknowledgements](#acknowledgements) -->
<!-- - [The tidyverse](#thetidyverse) -->
# Introduction
The ggplot2 package in the programming language R implements the "layered grammar of graphics" --- a proposal of Leland Wilkinson and the product of the dissertation and ongoing work of Hadley Wickham. This data visualization system idea is at once powerful, novel, and intuitive. Wickham and collaborators have built a system where data visualization is dividable into parameters: data to visualize, aesthetics that represent variables, geometric objects, a coordinate system, specific scales, and statistical transformation.
In this book, I use a "slow ggplotting" method as well as a "flipbook" format to facilitate the introduction of this plotting method. "Slow ggplotting" makes modifications to plots as incrementally as possible so that it is clear to users what code triggers each new layer or modification. The aim is less to be concise, and more to be explicit about how new statements modify the plot; repetition should facilitating more interactions with ggplotting to help newcomers internalize the functions. The flipbook layout is designed to show what code is triggering each change in the plot output.
Readers of the book should easily be able to follow along with how plots are built up. While intuitive, using ggplotting effectively and efficiently nevertheless requires practice. My hope is that these examples will be a helpful reference point for those that endeavor to pick up data visualization with ggplot2.
---
name: whatsslow
# What's the slow ggplot workflow?
"Slow ggplot" just means working more incrementally than is typical. Elements of the approach are as follows:
- pulling out aes() from the ggplot() function:
- using fewer functions; example - using labs() to add a title instead of ggtitle()
- using functions multiple times; example aes(x = var1) + aes(y = var2) rather than aes(x = var1, y = var2)
- using base R functions and tidyverse functions. For other packages, the :: style to call them
- write out arguments (no shortcuts) aes(x = gdppercap) not aes(gdppercap)
- order ggplot commands so that reactivity is obvious; scale adjustments to aesthetics might also be near the aesthetic declaration.
---
Here, I contrast the usual plotting method to slow ggplotting:
Usual approach:
```{r, eval = F}
ggplot(my_data, aes(var1, y = var2, col = var3)) +
geom_point() +
ggtitle("My Title") +
labs(x = "the x label", y = "the y label", col = "legend title")
```
Using slow ggplotting:
```{r, eval = F}
ggplot(data = my_data) +
aes(x = var1) +
labs(x = "the x label") +
aes(y = var2) +
labs(y = "the y label") +
geom_point() +
aes(col = var3) +
labs(col = "legend title") +
labs(title = "My title")
```
---
name: gettingstarted
# Getting started with R and RStudio
- [Install Windows](https://www.youtube.com/watch?v=aCRMhAWmtpw)
- [Install Mac](https://www.youtube.com/watch?v=GLLZhc_5enQ)
---
name: acknowledgements
# Acknowledgements
I'm grateful to the ggplot team for their tireless work on developing and maintaining this fantastic resource. Additionally most of the visualizations here were produced for the Tableau-users-associated initiative #MakeoverMonday. I'm grateful to that community for feedback and encouragement and for welcoming work from outside of Tableau. Additional visualizations have been made with data curated by #TidyTuesday, a similar initiative from RStudio. Many thanks are due to Emi Tanaka (@statsgen) and Garrick Aden-Buie (@grrrck) helped by writing the "reveal" function to made the flipbook style possible. Also to Yihui Xie and others that work on the Xaringan package for creating slides from markdown and R code; I'm using Emi Tanaka's themes kunoichi and ninjutsu.
While I'm hopeful that this book serves as a reference, I think the best introductions and learning happens in person. My formal introduction to ggplot2 was at The Zurich Summer School for Women in Political Methodology in 2017 at the University of Zürich. I'm very grateful to the organizers and presenters of that workshop; Denise Traber gave an excellent presention on ggplot2. I'm grateful too to my students who motivate me to think about how to teach what I've been so lucky to learn. And thanks to David, who is supportive of plotting and of me.
```{r, echo = F}
library(tidyverse)
library(printr)
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(fig.height = 6, out.width = "80%", comment = " ", cache = T, dpi = 300, warning = F, message = F)
source(file = "xaringan_reveal_parenthetical.R")
```
---
name:visualtoc
#Visual Table of Contents
```{r, results='asis', echo=FALSE}
make_html_picture_link("figures/christmas.png", "#christmastreesales")
make_html_picture_link("figures/baseball.png", "#baseball")
make_html_picture_link("figures/parent_leave.png", "#parentalleave")
make_html_picture_link("figures/traits.png", "#traits")
make_html_picture_link("figures/white_house.png", "#salaries")
make_html_picture_link("figures/winter_games.png", "#wintergamemedals")
```
```{r, results='asis', echo=FALSE}
make_html_picture_link("figures/brexit_1.png", "#brexit")
make_html_picture_link("figures/map_curry.png", "#britishburgers")
make_html_picture_link("figures/away_from_ben.png", "#britishfood")
make_html_picture_link("figures/life_expect.png", "#lifeexpect")
make_html_picture_link("figures/myers_briggs.png", "#myersbriggs")
make_html_picture_link("figures/women.png", "#womensrepresentation")
```
```{r, results='asis', echo=FALSE}
make_html_picture_link("figures/wine.png", "#wine")
make_html_picture_link("figures/ice.png", "#ice")
make_html_picture_link("figures/recent_grads_01.png", "#collegemajors01")
make_html_picture_link("figures/recent_grads_02.png", "#collegemajors02")
make_html_picture_link("figures/flipping_seats.png", "#flippingseats")
make_html_picture_link("figures/company_ranks.png", "#ratings")
```
[More on next page](#visualtoc2)
---
name: visualtoc2
#Visual Table of Contents
```{r, results='asis', echo=FALSE}
make_html_picture_link("figures/incarceration_01.png", "#incarcerationbysex")
make_html_picture_link("figures/incarceration_02.png", "#incarcerationbygender")
make_html_picture_link("figures/milk.png", "#milkindustry")
make_html_picture_link("figures/wind_01.png", "#windturbinelocations01")
make_html_picture_link("figures/wind_02.png", "#windturbinelocations02")
make_html_picture_link("figures/movies.png", "#movies")
```
---
---
```{r, child = list.files()[1], warning=F, message=F}
```
```{r, child = list.files()[2], warning=F, message=F}
```
---
# Hic sunt dracones!
This book is not complete. The plots that follow aren't as carefully edited as what precedes. But feel free to have a look around. This "here be dragons" slide should move later and later on as weeks go by. Also I hope to add new work as time goes on.
---
```{r, child = list.files(pattern = "\\d.+R|rmd")[3:20], warning=F, message=F}
```
```{css, eval = TRUE, echo = F}
.remark-code{line-height: 1.5; font-size: 65%}
```