You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: vignettes/articles/cookbook/_annotations.Rmd
+15-17Lines changed: 15 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,8 @@
2
2
3
3
Labelling your chart is often preferable to using a legend, as often this relies on a user matching the legend to the data using colour alone. The legend can be removed from a chart by setting `legend = "none"` in `theme_sg()`.
4
4
5
+
Either `geom_label()` or `geom_text()` can be used to annotate a chart. The main difference is that `geom_label()` draws a rectangle behind the text (white by default) and a border the same colour as the text (`label.size = NA` can be used to remove the border). `geom_text()` does not include a background and so text can be harder to read if it overlaps with other chart elements.
6
+
5
7
The easiest way to add an annotation is to manually define the co-ordinates of the required position.
6
8
7
9
```{r annotations-data}
@@ -10,24 +12,26 @@ ann_data <- gapminder |>
10
12
```
11
13
12
14
```{r annotations-1}
13
-
#| fig.alt = "A multiple line chart with colour co-ordinated line labels using sgplot theme and main palette."
15
+
#| fig.alt = "A multiple line chart using sgplot theme and main2 palette with lines labelled."
14
16
15
17
ann_data |>
16
-
ggplot(aes(x = year, y = lifeExp, colour = country)) +
annotate(geom = "label", x = 2008, y = 73, label = "China",
26
-
label.size = NA,
27
-
hjust = 0, vjust = 0.5) +
28
-
annotate(geom = "label", x = 2008, y = 79.4, label = "United Kingdom",
29
-
label.size = NA,
30
-
hjust = 0, vjust = 0.5) +
27
+
geom_label(x = 2008, y = 73, label = "China",
28
+
hjust = 0,
29
+
vjust = 0.5,
30
+
label.size = NA) +
31
+
geom_label(x = 2008, y = 79.4, label = "United Kingdom",
32
+
hjust = 0,
33
+
vjust = 0.5,
34
+
label.size = NA) +
31
35
labs(
32
36
x = "Year",
33
37
y = NULL,
@@ -39,17 +43,13 @@ ann_data |>
39
43
40
44
However, this makes the code difficult to reuse as values are hard coded and not automatically generated from the data. Automating the position of annotations is possible, but more fiddly.
41
45
42
-
The following examples use `geom_label()` to use values from the data to position annotations. `geom_label()` draws a rectangle behind the text (white by default) and a border the same colour as the text (`label_size = NA` can be used to remove the border). `geom_text()` is also an option for annotations, but this does not include a background and so can be harder for text to read if it overlaps with other chart elements. These functions also have `nudge` arguments that can be used to displace text to improve the positioning.
43
-
44
-
Note that in the previous examples, `annotate()` also requires a geom (`label` or `text`). These operate in the same way as `geom_label()` and `geom_text()`, but as discussed, `annotate()` is only able to deal with fixed values.
45
-
46
+
One way to automate this is to create a supplementary data frame with desired co-ordinates of the labels (see `ann_labs` in the example below). `nudge` arguments can be used to displace text to improve the positioning.
46
47
47
48
```{r annotations-2}
48
-
#| fig.alt = "A multiple line chart with colour co-ordinated line labels using sgplot theme and main2 palette."
49
+
#| fig.alt = "A multiple line chart using sgplot theme and main2 palette with lines labelled."
**Note**: The `annotate()` function should be used to add annotations with manually defined positioning co-ordinates, whereas `geom_label()` and `geom_text()` should be used when using co-ordinates defined in a data frame. Although the reverse may work, text can appear blurry.
0 commit comments