-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chapter15.qmd
333 lines (271 loc) · 7.97 KB
/
Chapter15.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
---
title: "Chapter 15"
subtitle: "Coordinate Systems"
author: "Aditya Dahiya"
date: "2024-03-04"
format:
html:
code-fold: true
code-copy: hover
code-link: true
execute:
echo: true
warning: false
error: false
filters:
- social-share
share:
permalink: "https://aditya-dahiya.github.io/ggplot2book3e/Chapter15.html"
description: "Solutions Manual (and Beyond) for ggplot2: Elegant Graphics for Data Analysis (3e)"
twitter: true
facebook: true
linkedin: true
email: true
mastodon: true
editor_options:
chunk_output_type: console
bibliography: references.bib
---
::: {.callout-note appearance="minimal"}
This chapter has no exercises, so I use a dummy dataset to generate some examples of the functions and ggplot2 customization of coordinates discussed in this chapter.
:::
```{r}
#| label: setup
library(tidyverse)
library(scales)
library(gt)
library(gtExtras)
```
To begin with, lets create a dummy data set shown in @fig-tb
```{r}
#| label: fig-tb
#| fig-cap: "A dummy dataset to use in the examples for this Chapters' Solutions / Examples"
set.seed(123)
tb <- tibble(
v_integer = 1:20,
v_squared = v_integer ^ 2,
v_regress = round(
(v_integer ^ 2) +
rnorm(
20, mean = 0, sd = 1),
2),
v_random = sample(1:100, 20, replace = FALSE),
v_discrete = sample(LETTERS[1:4], 20, replace = TRUE)
)
tb |>
gt() |>
cols_align("center") |>
gt_theme_538()
```
## 15.1 Linear coordinate systems
Let's use the example plot in @fig-sec15 to demonstrate the various changes / customization possible with ggplot2 with coordinates.
```{r}
#| label: fig-sec15
#| fig-cap: "An example plot which we will later use in various examples of tweaking the Linear Coordinate Systems"
g1 <- tb |>
ggplot(
aes(
x = v_integer,
y = v_regress
)
) +
geom_smooth(
se = TRUE,
colour = "grey",
fill = "lightgrey",
alpha = 0.5
) +
geom_point(
aes(colour = v_discrete),
size = 3
) +
scale_colour_brewer(palette = "Dark2") +
theme_minimal() +
theme(axis.line = element_line(arrow = arrow()))
g1
```
### 15.1.1 Zooming into a plot with `coord_cartesian()`
The @fig-zoom shows an example of zooming into a plot with `coord_cartesian()` and its difference from the other option using `scale_*_continuous(limits = *)` .
```{r}
#| label: fig-zoom
#| fig-cap: "Zooming in on a specified portion of Y-Axis: 100 to 200"
#| fig-subcap:
#| - "Base Plot"
#| - "Limits argument in scale_y_continuous()"
#| - "ylim argument in coord_cartesian()"
g1 +
labs(title = "The Base Plot shows entire data")
g1 + scale_y_continuous(limits = c(100, 200)) +
labs(title = "Using limits in scale_y_continuous() eliminates\nall data out of limits")
g1 + coord_cartesian(ylim = c(100, 200)) +
labs(title = "Using ylim in coord_cartesian() simply zooms-in,\nand preserves out-of-limits data")
```
### 15.1.2 Flipping the axes with `coord_flip()`
The @fig-flip shows examples of flipping the coordinates, and reasoning as to why it may be better than changing X-axis and Y-axis variables manually, especially if we are using `geom_smooth()` which uses `y ~ x` formula by default.
```{r}
#| label: fig-flip
#| fig-cap: "Demonstrating the use of coord_flip()"
#| fig-subcap:
#| - "The base plot"
#| - "Smoother line still follows v_regress ~ v_integer formula"
#| - "Smoother line now follows v_integer ~ v_regress formula"
g1 + labs(title = "The base plot",
subtitle = "Dependent variable is v_regress, and independent var is v_integer")
g1 +
coord_flip() +
labs(title = "coord_flip()",
subtitle = "Smoother line still follows v_regress ~ v_integer formula")
tb |>
ggplot(
aes(
y = v_integer,
x = v_regress
)
) +
geom_smooth(
se = TRUE,
colour = "grey",
fill = "lightgrey",
alpha = 0.5
) +
geom_point(
aes(colour = v_discrete),
size = 3
) +
scale_color_brewer(palette = "Dark2") +
theme_minimal() +
theme(axis.line = element_line(arrow = arrow())) +
labs(title = "Exchanging x and y variable",
subtitle = "Smoother line now follows v_integer ~ v_regress formula")
```
### 15.1.3 Equal scales with `coord_fixed()`
The @fig-coordfixed shows the same three graphs above, but in a much better sense now that coordinates are fixed to be equal.
```{r}
#| label: fig-coordfixed
#| fig-cap: "Demonstrating the use of coord_fixed()"
#| fig-subcap:
#| - "The base plot"
#| - "The base plot with fixed coordinates"
#| layout-ncol: 2
g1 + labs(title = "The base plot")
g1 + coord_fixed() + labs(title = "The base plot with coord_fixed()")
```
## 15.2 Non-linear coordinate systems
The @fig-coord1 demonstrates five types of coordinate systems - first three linear, and the last two non-linear.
```{r}
#| label: fig-coord1
#| fig-cap: "A demonstration of Polar Coordinates, Flipped Coordinates, Transformed Coordinates and Fixed Coordinates"
#| fig-subcap:
#| - "coord_cartesian()"
#| - "coord_flip()"
#| - "coord_fixed()"
#| - "coord_polar()"
#| - "coord_trans()"
tb <- tibble(
x_random = runif(100, min = 0, max = 100),
y_random = (1.5 * x_random) + (rnorm(100, 0, 25))
)
g1 <- ggplot(tb, aes(x_random, y_random)) +
geom_point() +
geom_smooth(se = FALSE) +
labs(title = "Cartesian Coordinates") +
theme_minimal()
g1
g1 + coord_flip() +
labs(title = "Flipped Coordinates",
subtitle = "coord_flipped()")
g1 + coord_fixed() +
labs(title = "Fixed Coordinates",
subtitle = "coord_fixed()")
g1 + coord_polar() +
labs(title = "Polar Coordinates",
subtitle = "coord_polar() transforms x-axis into theta (angle) and y-axis into radius")
g1 + coord_trans(x = "log10") +
labs(title = "Transformed Coordinates",
subtitle = "coord_trans() with x-axis on log10 scale")
```
### 15.2.1 Transformations with `coord_trans()`
The impact of `coord_trans()` can be demonstrated in @fig-c-trans shown below.
```{r}
#| label: fig-c-trans
#| fig-cap: "Use of coord_trans() to change the graph into a non-linear one"
g1 +
coord_trans(x = "log2") +
scale_x_continuous(breaks = 2^(1:8)) +
theme(panel.grid.minor = element_blank()) +
labs(
title = "Transformation to Log 2 scale",
subtitle = "Note that x-axis has equidistant powers of 2"
)
```
### 15.2.2 Polar coordinates with `coord_polar()`
The @fig-polar demonstrates various uses of `coord_polar()`
```{r}
#| label: fig-polar
#| fig-cap: "Using polar coordinates"
#| fig-subcap:
#| - "A Stacked bar chart"
#| - "Chart with coord_polar()"
#| - "Bulls-Eye Chart"
#| - "Pie-Chart"
tb2 <- tibble(
Type = sample(LETTERS[1:4], size = 10, replace = T)
)
g2 <- tb2 |>
ggplot(aes(x = "1", fill = Type)) +
geom_bar() +
labs(title = "A Stacked Bar-Chart") +
theme_void() +
theme(legend.position = "bottom")
g2
g2 +
coord_polar() +
labs(title = "coord_polar()",
subtitle = "Without use of expand = expansion(0)")
g2 +
coord_polar() +
scale_x_discrete(expand = expansion(0)) +
labs(title = "Bulls-Eye Chart",
subtitle = "Using expand = expansion(0) in scale_x_discrete()")
g2 +
coord_polar(theta = "y") +
labs(title = "Pie Chart",
subtitle = "Using theta = 'y' in coord_polar()")
```
### 15.2.3 Map projections with `coord_map()`
Here, in @fig-map, we show the map of Canada with different projections.
```{r}
#| label: fig-map
#| fig-cap: "Map of Canada with different coordinates"
#| fig-subcap:
#| - "Base map in coord_cartesian()"
#| - "coord_quickmap()"
#| - "coord_map()"
#| - "coord_map(projection = `ortho`)"
g3 <- map_data(
database = "world",
regions = "canada"
) |>
ggplot(
aes(
x = long,
y = lat,
group = group
)
) +
geom_polygon(
fill = "white",
colour = "black"
) +
theme(legend.position = "none") +
labs(title = "Canada's Map: coord_cartesian()")
g3 +
coord_quickmap() +
labs(title = "Canada's Map: coord_quickmap()")
g3 +
coord_map() +
labs(title = "Canada's Map: coord_map()")
g3 +
coord_map("ortho") +
labs(title = "Canada's Map: coord_map(projection = `ortho`)")
```