-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chapter14.qmd
283 lines (236 loc) · 7.49 KB
/
Chapter14.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
---
title: "Chapter 14"
subtitle: "Scales and guides"
author: "Aditya Dahiya"
date: "2024-03-03"
format:
html:
code-fold: true
code-copy: hover
code-link: true
execute:
echo: true
warning: false
error: false
cache: true
filters:
- social-share
share:
permalink: "https://aditya-dahiya.github.io/ggplot2book3e/Chapter14.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 (yet!), so we show some examples to demonstrate the customization of scales.
:::
```{r}
#| label: setup
library(tidyverse)
library(scales)
library(ggnewscale)
library(gt)
library(gtExtras)
library(cowplot)
```
A dummy data set to use for demonstrating scales in ggplot2 is shown in @fig-data
```{r}
#| label: fig-data
#| fig-cap: "Dummy data set created for demonstration of scales in ggplot2"
tb <- tibble(
v_random_number = sample(0:100, size = 10, replace = FALSE),
v_continuous = 1:10,
v_discrete = sample(letters, 10),
v_expo = (1:10)^4
)
tb |> gt() |> gt_theme_538()
```
## 14.1 Theory of scales and guides
Scale specification, naming scheme and fundamental scale types are shown in @fig-1.
```{r}
#| label: fig-1
#| fig-cap: "The demonstration of Scale specification, naming scheme and fundamental scale types in ggplot2"
g <- tb |>
ggplot(
aes(
x = v_continuous,
y = v_random_number,
colour = v_discrete
)
) +
geom_point(size = 4) +
paletteer::scale_color_paletteer_d(
"palettetown::croconaw"
) +
labs(
subtitle = "ggplot2 automatically assigns scale_*_continuous() to X and Y axis in this plot.\nThe colour scale becomes scale_colour_discrete()"
) +
theme_cowplot() +
theme(
legend.position = "bottom"
)
g
```
## 14.2 Scale names
The scales can be easily names, wither in the main `scale_*_*()` function using the argument `name = "..."` , or, more easily using the `labs()` helper function as shown below in @fig-scale-names
```{r}
#| label: fig-scale-names
#| fig-cap: "Using labs() helper function to add a title to legends and axis"
g +
labs(
title = "A nice graph with a title",
x = "A continuous variable from 1 to 10",
y = "A number (1 to 100)",
colour = "A discrete variable",
subtitle = "The labs() function helps to easily add titles, subtites, legend titles and axes titles."
)
```
## 14.3 Scale breaks
The `breaks =` argument to the `scale_*_*()` functions is useful in determining the axis-ticks and lines or the colour levels in the legend to be displayed, as shown below in @fig-breaks
```{r}
#| label: fig-breaks
#| fig-cap: "Using breaks argument in scale_*_* functions to show customized intervals of 2 on x-axis, and, 1000 on y-axis"
g1 <- tb |>
ggplot(aes(x = v_continuous,
y = v_expo,
colour = v_discrete)) +
geom_point(
size = 6
) +
scale_x_continuous(
breaks = seq(0, 10, 2),
name = "Breaks at even numbers"
) +
scale_y_continuous(
name = "Exponentially increasing numbers\nBreaks at interval of 2000",
breaks = seq(0, 10000, 2000)
) +
labs(
title = "Using breaks argument in scale_*_* functions",
subtitle = "This plot shows customized breaks at interval of 2 on x-axis and 1000 on y-axis"
) +
theme_cowplot() +
theme(legend.position = "none")
g1
```
## 14.4 Scale limits
The @fig-limits shows an example of setting scale limits using the `limit =` argument in `scale_*_*()` functions in ggplot2. In this example, Y-axis limited upto 8000; and colour scale limtied to levels between `a` to `h` only.
```{r}
#| label: fig-limits
#| fig-cap: "Setting scale limits using limit argument in scale_*_* functions: Y-axis limited upto 8000; and colour scale limtied to some levels only"
g2 <- g1 +
scale_y_continuous(
limits = c(0, 8000),
name = "Limits from 0 to 8000"
) +
scale_colour_discrete(
limits = letters[1:8],
na.value = "lightgrey",
name = "Limited to characters between a to h"
) +
labs(
title = "Setting scale limits",
subtitle = "Y-axis limited upto 8000; and colour scale limtied to a - h only"
) +
theme(legend.position = "bottom")
g2
```
Further, with the `oob =` argument we can control the behavior of out-of-bounds data, as shown in @fig-oob below to squish to data onto the nearest limits.
```{r}
#| label: fig-oob
#| fig-cap: "Using oob argument to limit Y-axis up-to 8000, but with out of bounds value squished to 8000"
g2 +
scale_y_continuous(
limits = c(0, 8000),
name = "Limits from 0 to 8000; with squish oob",
oob = squish
) +
labs(
subtitle = "Y-axis limited upto 8000, but with out of bounds value squished to 8000"
)
```
## 14.5 Scale guides
To demonstrate the use of guides() function, we customize the colour legend from @fig-breaks to generate a customized legend in @fig-guides
```{r}
#| label: fig-guides
#| fig-cap: "Using guides() with guide_legend() to customize legend"
g4 <- g1 +
theme(legend.position = "bottom") +
labs(
title = "Customized Legend",
colour = "Using guide_legend() to customize legend") +
guides(
colour = guide_legend(
direction = "vertical",
reverse = TRUE,
nrow = 2,
override.aes = list(
size = 4
),
theme = theme(
legend.text = element_text(
face = "bold"
)
)
)
)
g4
```
## 14.6 Scale transformation
The continuous scales in @fig-guides can be transformed using the `trans` argument in the `scale_*_*()` function, as shown in @fig-trans below.
```{r}
#| label: fig-trans
#| fig-cap: "Continuous scale transformation into a log2 scale using trans argument in the scale_*_*() function"
g4 +
scale_y_continuous(
trans = "log2",
name = "Transformed Y-Axis\nwith powers of Two"
) +
labs(subtitle = "Transformed Y-Axis to the powers of 2",
title = "Transformed Axes")
```
## 14.7 Legend merging and splitting
The @fig-merged shows us that if we’ve mapped colour to both points and lines, the keys will show both points and lines.
```{r}
#| label: fig-merged
#| fig-cap: "Merged legends in ggplot2 - the default behaviour"
g4 +
geom_col(
fill = "transparent"
) +
labs(subtitle = "Merged Legends - default option in ggplot2",
title = "Legend merging and splitting (1)",
colour = "Merged Legends")
```
The @fig-split shows us how we can split or hide legends for certain geoms - here we-ve hidden the colour legend for lines / column bars - so the key will show only points (and not lines).
```{r}
#| label: fig-split
#| fig-cap: "Hiding specific legends using show.legend = FALSE in one of the geoms"
g4 +
geom_col(
fill = "transparent",
show.legend = FALSE
) +
labs(subtitle = "Splitting / Hiding Legends using show.legend = FALSE in one of the geoms",
title = "Legend merging and splitting (2)",
colour = "Legend only for points")
```
## 14.8 Legend key glyphs
Using the same graph as the last section, the @fig-glyph demonstrates the use of argument `key_glyph =` within a `geom_*()` function to change the glyph used in the key of the graph.
```{r}
#| label: fig-glyph
#| fig-cap: "The use of argument key_glyph with a geom_*() to change the glyph used in the key"
g4 +
geom_col(
fill = "transparent",
key_glyph = draw_key_path
) +
labs(title = "Changing the glyphs for legends",
colour = "Customized legend glyph")
```