-
Notifications
You must be signed in to change notification settings - Fork 5
/
010-data_vis.Rmd
293 lines (210 loc) · 6.25 KB
/
010-data_vis.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
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
# Data Visualization
Data visualization Chapter in r4ds
- http://r4ds.had.co.nz/data-visualisation.html
Datacamp Courses:
- The `ggplot2` stack
- https://www.datacamp.com/courses/data-visualization-with-ggplot2-1
- https://www.datacamp.com/courses/data-visualization-with-ggplot2-2
- https://www.datacamp.com/courses/data-visualization-with-ggplot2-part-3
## Loading ggplot2
```{r}
library(ggplot2)
```
```{r}
# mpg dataset from the ggplot2 library
mpg
```
## Creating a ggplot
```{r}
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy))
```
## Aesthetic Mapings
```{r}
# using color
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, color = class))
```
```{r}
# using size
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, size = class))
```
```{r}
# using alpha
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, alpha = class))
```
```{r}
# using shape
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy, shape = class))
```
```{r}
# manual set property
# note color is not in the aes
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy), color = "blue")
```
```{r, eval=FALSE}
# if you put a variable outside you will get an error
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy), color = class)
```
## Facets
```{r}
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy)) + facet_wrap(~ class, nrow = 2)
```
```{r}
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy)) + facet_grid(drv ~ cyl)
```
## Geometic Objects
```{r}
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy))
```
```{r}
ggplot(data = mpg) + geom_smooth(mapping = aes(x = displ, y = hwy))
```
```{r}
ggplot(data = mpg) + geom_smooth(mapping = aes(x = displ, y = hwy, linetime = drv))
```
```{r}
# base plot before groupings
ggplot(data = mpg) + geom_smooth(mapping = aes(x = displ, y = hwy))
```
```{r}
# base plot before groupings
ggplot(data = mpg) + geom_smooth(mapping = aes(x = displ, y = hwy))
# separate smoothing line by group
ggplot(data = mpg) + geom_smooth(mapping = aes(x = displ, y = hwy, group = drv))
# different color foe each group
ggplot(data = mpg) + geom_smooth(mapping = aes(x = displ, y = hwy, color = drv), show.legend = FALSE)
```
Adding multiple geoms in the same plot
```{r}
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
geom_smooth(mapping = aes(x = displ, y = hwy))
```
The layering system will carry over values from the previous layer.
the ggplot layer will specify the global values
```{r}
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
geom_point() +
geom_smooth()
```
Mappings in a a geom function, will overwrite the global settings (i.e., they are local settings)
```{r}
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
geom_point(mapping = aes(color = class)) +
geom_smooth()
```
```{r}
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
geom_point(mapping = aes(color = class)) +
geom_smooth(
data = dplyr::filter(mpg, class == 'subcompact'),
se = FALSE
)
```
## Statistical Transformations
```{r}
dim(diamonds)
```
```{r}
head(diamonds)
```
```{r}
ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut))
```
Use a *stat* to calculate a new value.
diamonds data gets "transformed" into a frequency table that get's plotted by the bar plot.
Look at the geom_bar documentation, you will see the `stat` will be `count` (i.e., `stat_count()`).
```{r}
ggplot(data = diamonds) + stat_count(mapping = aes(x = cut))
```
You can set stat to 'identity' if you have already calculated a frequency table
```{r}
pre_counted <- tibble::as.tibble(table(diamonds$cut))
```
```{r}
ggplot(data = pre_counted) +
geom_bar(
mapping = aes(x = Var1, y = n), stat = 'identity'
)
```
```{r}
# overwrite default stat
# proportion instead of count
ggplot(data = diamonds) +
geom_bar(
mapping = aes(x = cut, y = ..prop.., group = 1)
)
```
grouping: http://ggplot2.tidyverse.org/reference/aes_group_order.html
By default, the group is set to the interaction of all discrete variables in the
plot. This often partitions the data correctly, but when it does not, or when
no discrete variable is used in the plot, you will need to explicitly define the
grouping structure, by mapping group to a variable that has a different value
for each group.
## Position Adjustments
```{r}
# using color
ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, color = cut))
```
```{r}
# using fill
ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, fill = cut))
```
```{r}
# another example of fill
# fill a different variable than x
# This creates a stacked bar chart
ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, fill = clarity))
```
position: identity
```{r}
ggplot(data = diamonds, mapping = aes(x = cut, fill = clarity)) + geom_bar(alpha = 1/5, position = 'identity')
```
```{r}
ggplot(data = diamonds, mapping = aes(x = cut, color = clarity)) + geom_bar(fill = NA, position = 'identity')
```
potition: fill
```{r}
ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, fill = clarity), position = 'fill')
```
position: dodge
```{r}
ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, fill = clarity), position = 'dodge')
```
Jitter scatter plot
```{r}
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy))
```
```{r}
ggplot(data = mpg) + geom_point(mapping = aes(x = displ, y = hwy), position = 'jitter')
```
## Coordinate Systems
coord_flipswaps the x and y axis, useful when you have long labels
```{r}
ggplot(data = mpg, mapping = aes(x = class, y = hwy)) + geom_boxplot()
```
```{r}
ggplot(data = mpg, mapping = aes(x = class, y = hwy)) + geom_boxplot() + coord_flip()
```
coord_quickmap, sets aspect ratio for maps
```{r}
usa <- ggplot2::map_data('usa')
```
```{r}
ggplot(usa, aes(long, lat, group = group)) + geom_polygon(fill = 'white', color = 'black')
```
```{r}
ggplot(usa, aes(long, lat, group = group)) + geom_polygon(fill = 'white', color = 'black') + coord_quickmap()
```
corrd_polar, uses polar coordinates
```{r}
bar <- ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = cut), show.legend = FALSE, width = 1) +
theme(aspect.ratio = 1) +
labs(x = NULL, y = NULL)
```
```{r}
bar + coord_flip()
```
```{r}
bar + coord_polar()
```