generated from jhudsl/OTTR_Template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05-data-visualization.Rmd
194 lines (120 loc) · 6.69 KB
/
05-data-visualization.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
188
189
190
191
192
193
194
# Data Visualization
```{r, echo=F, message=F, warning=F, error=F}
install.packages("palmerpenguins", repos = "http://cran.us.r-project.org")
```
```{r echo=F, message=F, warning=F, error=F}
library(tidyverse)
library(palmerpenguins)
```
Now that we have learned basic data structures in R, we can now learn about how to do visualize our data. There are several different data visualization tools in R, and we focus on one of the most popular, "Grammar of Graphics", or known as "ggplot".
The syntax for `ggplot2` will look a bit different than the code we have been writing, with syntax such as:
```{r, eval=FALSE}
ggplot(penguins) + aes(x = bill_length_mm) + geom_histogram()
# Data Aesthetics Geometry
```
The output of all of these functions, such as from `ggplot()` or `aes()` are not data types or data structures that we are familiar with...rather, they are graphical information.
You should be worried less about how this syntax is similar to what we have learned in the course so far, but to view it as a new grammar (of graphics!) that you can "layer" on to create more sophisticated plots.
![](images/khealy_ggplot1_part1.jpg)
To get started, we will consider these most simple and common plots:
**Univariate**
- Numeric: histogram
- Character: bar plots
**Bivariate**
- Numeric vs. Numeric: Scatterplot, line plot
- Numeric vs. Character: Box plot
Why do we focus on these common plots? Our eyes are better at distinguishing certain visual features more than others. All of these plots are focused on their position to depict data, which gives us the most effective visual scale.
![Image Source: <https://www.oreilly.com/library/view/visualization-analysis-and/9781466508910/K14708_C005.xhtml>](https://www.oreilly.com/api/v2/epubs/9781466508910/files/image/fig5-1.png)
## Grammar of Graphics
The syntax of the grammar of graphics breaks down into 4 sections.
[Data]{style="color:orange"}
[Mapping to data]{style="color:green"}
[Geometry]{style="color:blue"}
[Additional settings]{style="color:purple"}
You add these 4 sections together to form a plot.
## Histogram
[ggplot(penguins)]{style="color:orange"} + [aes(x = bill_length_mm)]{style="color:green"} + [geom_histogram()]{style="color:blue"}
```{r, echo=F, warning=F, message=F}
ggplot(penguins) +
aes(x = bill_length_mm) +
geom_histogram() +
theme_bw()
```
## Let's take it apart
You can always try out a ggplot incrementally if you're not sure what pieces do:
```{r}
ggplot(penguins) #data
```
```{r}
ggplot(penguins) + #data
aes(x = bill_length_mm) #aesthetics
```
```{r}
ggplot(penguins) + #data
aes(x = bill_length_mm) + #aesthetics
geom_histogram() #geometry
```
## Bar plots
[ggplot(penguins)]{style="color:orange"} + [aes(x = species)]{style="color:green"} + [geom_bar()]{style="color:blue"}
```{r, echo=F, warning=F, message=F}
ggplot(penguins) + aes(x = species) + geom_bar()
```
### Scatterplot
[ggplot(penguins)]{style="color:orange"} + [aes(x = bill_length_mm, y = bill_depth_mm)]{style="color:green"} + [geom_point()]{style="color:blue"}
```{r, echo=F, warning=F, message=F}
ggplot(penguins) + aes(x = bill_length_mm, y = bill_depth_mm) + geom_point()
```
### Multivariate Scatterplot
[ggplot(penguins)]{style="color:orange"} + [aes(x = bill_length_mm, y = bill_depth_mm, color = species)]{style="color:green"} + [geom_point()]{style="color:blue"}
```{r, echo=F, warning=F, message=F}
ggplot(penguins) + aes(x = bill_length_mm, y = bill_depth_mm, color = species) + geom_point()
```
### Multivaraite Scatterplot
[ggplot(penguins)]{style="color:orange"} + [aes(x = bill_length_mm, y = bill_depth_mm)]{style="color:green"} + [geom_point()]{style="color:blue"} + [facet_wrap(\~species)]{style="color:purple"}
```{r, echo=F, warning=F, message=F}
ggplot(penguins) + aes(x = bill_length_mm, y = bill_depth_mm) + geom_point() + facet_wrap(~species)
```
### Line plot?
[ggplot(penguins)]{style="color:orange"} + [aes(x = bill_length_mm, y = bill_depth_mm)]{style="color:green"} + [geom_line()]{style="color:blue"}
```{r, echo=F, warning=F, message=F}
ggplot(penguins) + aes(x = bill_length_mm, y = bill_depth_mm) + geom_line()
```
### Grouped Line plot?
[ggplot(penguins)]{style="color:orange"} + [aes(x = bill_length_mm, y = bill_depth_mm, group = species)]{style="color:green"} + [geom_line()]{style="color:blue"}
```{r, echo=F, warning=F, message=F}
ggplot(penguins) + aes(x = bill_length_mm, y = bill_depth_mm, group = species) + geom_line()
```
### Boxplot
[ggplot(penguins)]{style="color:orange"} + [aes(x = species, y = bill_depth_mm)]{style="color:green"} + [geom_boxplot()]{style="color:blue"}
```{r, echo=F, warning=F, message=F}
ggplot(penguins) + aes(x = species, y = bill_depth_mm) + geom_boxplot()
```
### Grouped Boxplot
[ggplot(penguins)]{style="color:orange"} + [aes(x = species, y = bill_depth_mm, color = island)]{style="color:green"} + [geom_boxplot()]{style="color:blue"}
```{r, echo=F, warning=F, message=F}
ggplot(penguins) + aes(x = species, y = bill_depth_mm, color = island) + geom_boxplot()
```
### Some additional options
[ggplot(data = penguins)]{style="color:orange"} + [aes(x = bill_length_mm, y = bill_depth_mm, color = species)]{style="color:green"} + [geom_point()]{style="color:blue"} + [labs(x = "Bill Length", y = "Bill Depth", title = "Comparison of penguin bill length and bill depth across species") + scale_x_continuous(limits = c(30, 60))]{style="color:purple"}
```{r, echo=F, warning=F, message=F}
ggplot(data = penguins) + geom_point(mapping = aes(x = bill_length_mm, y = bill_depth_mm, color = species)) + labs(x = "Bill Length", y = "Bill Depth", title = "Comparison of penguin bill length and bill depth across species") + scale_x_continuous(limits = c(30, 60))
```
## Summary of options
[data]{style="color:orange"}
\
[geom_point]{style="color:blue"}: [x, y, color, shape]{style="color:green"}
[geom_line]{style="color:blue"}: [x, y, group, color]{style="color:green"}
[geom_histogram]{style="color:blue"}: [x, y, fill]{style="color:green"}
[geom_bar]{style="color:blue"}: [x, fill]{style="color:green"}
[geom_boxplot]{style="color:blue"}: [x, y, fill, color]{style="color:green"}
\
[facet_wrap]{style="color:purple"}
\
[labs]{style="color:purple"}
[scale_x_continuous]{style="color:purple"}
[scale_y_continuous]{style="color:purple"}
[scale_x_discrete]{style="color:purple"}
[scale_y_discrete]{style="color:purple"}
Consider the `esquisse` package to help generate your ggplot code via drag and drop.
An excellent ggplot "cookbook" can be found [here](https://r-graphics.org/).
## Exercises
You can find [exercises and solutions on Posit Cloud](https://posit.cloud/content/8245357), or on [GitHub](https://github.com/fhdsl/Intro_to_R_Exercises).