-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8_label_formatting.qmd
More file actions
157 lines (128 loc) · 4.72 KB
/
8_label_formatting.qmd
File metadata and controls
157 lines (128 loc) · 4.72 KB
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
---
title: "Formatting axis labels"
author: "Charlotte Soneson, Michael Stadler"
format:
html:
toc: true
toc-location: left
embed-resources: true
link-external-icon: true
code-fold: true
editor_options:
chunk_output_type: console
---
## Summary
This document provides some examples of how to format various types of labels
in `ggplot2` plots.
## Prepare data
Run the following code to load the packages and data that are used in this
document:
```{r}
#| label: prepare_data
#| code-fold: false
suppressPackageStartupMessages({
library(dplyr)
library(tibble)
library(swissknife)
library(ggplot2)
library(ggtext)
library(ggrepel)
})
loadExampleData("mycars")
mycars <- mycars |>
rownames_to_column("model")
tibble(mycars)
```
## Construct basic plot
Using the `mycars` data, create the following basic plot.
```{r}
#| label: make_basic_plot
ggplot(mycars,
aes(x = mpg, y = disp, color = cyl)) +
geom_point(size = 4) +
scale_color_manual(values = c(`4` = "purple", `6` = "orange",
`8` = "forestgreen"),
name = "Number of\ncylinders") +
labs(x = "Miles/gallon", y = "Displacement") +
theme_bw(base_size = 13)
```
## Add labels to points
The first extension we will make to the basic plot is to add a label to each
point. We will use the `model` column. Note that the labels are added in such a
way that they do not overlap (more precisely, different labels are designed to
repel each other to avoid overlaps whenever possible).
```{r}
ggplot(mycars,
aes(x = mpg, y = disp, color = cyl, label = model)) +
geom_point(size = 4) +
geom_text_repel(min.segment.length = 0, max.overlaps = Inf) +
scale_color_manual(values = c(`4` = "purple", `6` = "orange",
`8` = "forestgreen"),
name = "Number of\ncylinders") +
labs(x = "Miles/gallon", y = "Displacement") +
theme_bw(base_size = 13)
```
## Format axis labels using markdown
Next, we turn our attention to the axis labels. The `ggtext` package provides
functionality to format the axes (or other text elements) using _markdown_.
Use this functionality to add formatting (italics and bold) to the x-axis title,
and add a unit to the y-axis title (here, it is helpful to note that HTML
code can be interpreted by markdown).
```{r}
ggplot(mycars,
aes(x = mpg, y = disp, color = cyl, label = model)) +
geom_point(size = 4) +
geom_text_repel(min.segment.length = 0, max.overlaps = Inf) +
scale_color_manual(values = c(`4` = "purple", `6` = "orange",
`8` = "forestgreen"),
name = "Number of\ncylinders") +
labs(x = "_Miles/gallon_ (**mpg**)", y = "Displacement (in<sup>3</sup>)") +
theme_bw(base_size = 13) +
theme(axis.title.x.bottom = element_markdown(),
axis.title.y.left = element_markdown())
```
## Add special characters to labels
Next, we will change the axis titles completely (the x-axis title to $\alpha$,
and the y-axis title to $\beta^4$). Note that these labels don't necessarily
make much sense in this case, but they serve as a useful example for general
special characters. We can achieve our goal in several ways - for example,
using the R `expression()` function:
```{r}
## Using expression
ggplot(mycars,
aes(x = mpg, y = disp, color = cyl, label = model)) +
geom_point(size = 4) +
geom_text_repel(min.segment.length = 0, max.overlaps = Inf) +
scale_color_manual(values = c(`4` = "purple", `6` = "orange",
`8` = "forestgreen"),
name = "Number of\ncylinders") +
labs(x = expression(~ alpha), y = expression(~ beta ^ 4)) +
theme_bw(base_size = 13)
```
or by using
[Unicode characters](https://en.wikipedia.org/wiki/List_of_Unicode_characters#)
characters (note that these may not be properly displayed unless the plot
backend is set to Cairo):
```{r}
#| dev: CairoPNG
## Using Unicode
ggplot(mycars,
aes(x = mpg, y = disp, color = cyl, label = model)) +
geom_point(size = 4) +
geom_text_repel(min.segment.length = 0, max.overlaps = Inf) +
scale_color_manual(values = c(`4` = "purple", `6` = "orange",
`8` = "forestgreen"),
name = "Number of\ncylinders") +
labs(x = "\U03B1", y = "\U03B2\U2074") +
theme_bw(base_size = 13)
```
## Resources
* Some
[useful examples](https://coderclub.w.uib.no/2015/05/07/expressions-in-r/) of
using `expression` and `bquote` to add special characters to plot labels.
Also see `?plotmath`.
## Session info
```{r}
#| label: session_info
sessioninfo::session_info()
```