-
Notifications
You must be signed in to change notification settings - Fork 16
/
03-individual_geoms.Rmd
260 lines (197 loc) · 6.57 KB
/
03-individual_geoms.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
# (PART\*) Layers {-}
# Individual Geoms
**Learning objectives**
* Discuss how geoms are the fundamental building blocks of ggplot2.
* Draw comparisons between geoms and their associated named plot.
* Explore each individual geom by reviewing their documentation.
```{r 03-individualgeoms-setup, include=FALSE}
# ggplot2 is attached with the tidyverse
library(tidyverse)
# some example data
df <- data.frame(
c = c(3, 1, 5),
y = c(2, 4, 6),
label = c("a", "b", "c")
)
```
## The basics
* Each geom can be useful by itself.
* Geoms can be used in ways to construct more complex geoms.
* The geoms discussed in this chapter are two dimensional (e.g., `x` and `y`).
* All geoms understand `color` or `colour` and size aesthetics.
* Bar, tile, and polygon understand `fill`.
* The terms above are all parameters within ggplot2 functions.
## Area chart: `geom_area()`
* Draws an area plot.
- A line plot filled to the y-axis.
- Multiple groups are stacked.
```{r}
ggplot(diamonds, aes(x = price)) +
geom_area(stat = "bin")
```
```{r}
ggplot(diamonds, aes(x = price, fill = cut)) +
geom_area(stat = "bin")
```
## Bar chart: `geom_bar()`
* Makes a bar plot.
```{r}
ggplot(diamonds, aes(cut)) +
geom_bar()
```
* What's up with `stat = "identity"`?
- The default stat is to count values.
- Setting this parameter leaves the data unchanged.
```{r}
# Why, though? Perhaps I want to do my own aggregation
data_diamond_count <-
diamonds |>
count(cut, name = "count")
ggplot(data_diamond_count, aes(cut, count)) +
geom_bar(stat = "identity")
```
## Line chart: `geom_line()`
* A geom that connects points from left to right.
- `linetype` is a useful parameter.
- Checkout the different linetypes [here](https://ggplot2.tidyverse.org/reference/aes_linetype_size_shape.html).
- Also here `?linetype`
```{r}
ggplot(economics, aes(x = date, y = unemploy)) +
geom_line()
```
* What's up with `geom_path()`?
- Connects points as they appear in order of the data
- Answer to exercise 2.
```{r}
ggplot(df, aes(c, y)) +
geom_path()
```
```{r}
ggplot(economics, aes(unemploy / pop, psavert)) +
geom_path()
```
## Scatterplot: `geom_point()`
```{r 03-scatter}
ggplot(mpg, aes(x = displ, y = hwy)) +
geom_point()
```
* The `shape` parameter is useful here.
- interested in the different shapes? `?shape`
```{r 03-scatter-dif-shape}
ggplot(mpg, aes(x = displ, y = hwy, shape = factor(cyl))) +
geom_point()
```
## Polygons: `geom_polygon()`
* Draws polygons, which are filled paths.
* Useful when making maps: more in [Chapter 6](https://ggplot2-book.org/maps).
```{r}
ggplot(df, aes(c, y)) +
geom_polygon()
```
## Histograms: `geom_histogram()`
```{r 03-hist}
ggplot(mpg, aes(hwy)) +
geom_histogram()
```
## Drawing rectangles: `geom_rect()`; `geom_tile()`; `geom_raster()`
```{r}
ggplot(df, aes(c, y)) +
geom_tile()
```
## Add text to a plot: `geom_text()`
* This requires the use of the `label` aesthetic, along with others
```{r}
# Filtering to simplify the example
mpg |>
filter(manufacturer == "ford") |>
ggplot(aes(displ, hwy, label = model)) +
geom_text()
```
* `position` and other parameters are also useful.
```{r}
mpg |>
filter(manufacturer == "ford") |>
ggplot(aes(displ, hwy, label = model)) +
geom_text(position = position_dodge(width = 0.2), angle = 45)
```
## Exercise solutions
### Exercise 1
* What geoms would you use to draw each of the following named plots?
- scatterplot = `geom_point()`
- line chart = `geom_line()`
- histogram = `geom_histogram()`
- bar chart = `geom_bar()` or `geom_col()`
- pie chart = `geom_bar()` with `coord_polar()`
```{r}
ggplot(data_diamond_count, aes(cut, count)) +
geom_col()
```
```{r}
ggplot(diamonds, aes(x = factor(1), fill = factor(cut))) +
geom_bar(width = 1) +
coord_polar(theta = "y")
```
### Exercise 2
* `geom_path()` connects points in order of appearance. `geom_line` connects points from left to right.
```{r 03-df, include=FALSE}
df <- data.frame(
x = c(3, 1, 5),
y = c(2, 4, 6),
label = c("a","b","c")
)
p <- ggplot(df, aes(x, y, label = label)) +
labs(x = NULL, y = NULL) + # Hide axis label
theme(plot.title = element_text(size = 12))
```
```{r 03-path}
p + geom_path()
```
* `geom_polygon()` draws polygons which are filled paths.
```{r 03-poly}
p + geom_polygon()
```
* `geom_line()` connects points from left to right.
```{r 03-line}
p + geom_line()
```
### Exercise 3
* What low-level geoms are used to draw geom_smooth()?
- `geom_smooth()` fits a smoother to data, displaying the smooth and its standard error, allowing you to see a dominant pattern within a scatterplot with a lot of "noise". The low level geom for `geom_smooth()` are `geom_path()`, `geom_area()` and `geom_point()`.
```{r 03-smooth}
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
geom_smooth()
```
* What low-level geoms are used to draw geom_boxplot()?
- Box plots are used to summarize the distribution of a set of points using summary statistics. The low level geom for `geom_boxplot()` are `geom_rect()`, `geom_line()` and `geom_point()`.
```{r 03-box}
ggplot(mpg, aes(drv, hwy)) +
geom_boxplot()
```
* What low-level geoms are used to draw geom_violin()?
- Violin plots show a compact representation of the density of the distribution highlighting the areas where most of the points are found. The low level geom for `geom_violin()` are `geom_area()` and `geom_path()`.
```{r 03-violin}
ggplot(mpg, aes(drv, hwy)) +
geom_violin()
```
## Meeting Videos
### Cohort 1
`r knitr::include_url("https://www.youtube.com/embed/XcTYBH4XbHo")`
<details>
<summary> Meeting chat log </summary>
```
00:13:39 priyanka gagneja: I forgot to mention that since this is relatively smaller chapter Ryan has prepared some material introducing Chapter 4 for today and he will talking about the entire chapter next week.
00:16:38 priyanka gagneja: that's correct
00:16:42 priyanka gagneja: that's my understanding too
00:18:38 priyanka gagneja: what do you mean circles .. can you share a more detailed example
00:21:59 Jiwan Heo: tibble(id = 1:10) %>% mutate(x = cos(2*pi*id/10), y = sin(2*pi*id/10)) %>% ggplot(aes(x, y)) + geom_line() + coord_equal()
00:22:05 Jiwan Heo: vs tibble(id = 1:10) %>% mutate(x = cos(2*pi*id/10), y = sin(2*pi*id/10)) %>% ggplot(aes(x, y)) + geom_path() + coord_equal()
00:35:42 priyanka gagneja: Thank you Ryan !!
00:38:16 priyanka gagneja: need a min
00:52:22 Michael Haugen: “Side rail” no pun intended
00:52:34 Ryan S: lol
00:52:34 Michael Haugen: sounds great
00:54:02 Ryan Metcalf: I was going to use Derail…..no pun intended!
00:54:05 Ryan Metcalf: Thanks you everyone!
```
</details>