Skip to content

Commit 306f652

Browse files
committed
Use geom_label for fixed co-ords (instead of annotate) and review text (#72)
1 parent 703b2a3 commit 306f652

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

vignettes/articles/cookbook/_annotations.Rmd

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
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()`.
44

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+
57
The easiest way to add an annotation is to manually define the co-ordinates of the required position.
68

79
```{r annotations-data}
@@ -10,24 +12,26 @@ ann_data <- gapminder |>
1012
```
1113

1214
```{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."
1416
1517
ann_data |>
16-
ggplot(aes(x = year, y = lifeExp, colour = country)) +
17-
geom_line(linewidth = 1) +
18+
ggplot() +
19+
geom_line(aes(x = year, y = lifeExp, colour = country), linewidth = 1) +
1820
theme_sg(legend = "none") +
1921
scale_colour_discrete_sg() +
2022
scale_y_continuous(limits = c(0, 82),
2123
breaks = seq(0, 80, 20),
2224
expand = c(0, 0)) +
2325
scale_x_continuous(limits = c(1952, 2017),
2426
breaks = seq(1952, 2007, 5)) +
25-
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) +
3135
labs(
3236
x = "Year",
3337
y = NULL,
@@ -39,17 +43,13 @@ ann_data |>
3943

4044
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.
4145

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.
4647

4748
```{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."
4950
5051
ann_labs <- ann_data |>
5152
group_by(country) |>
52-
mutate(min_year = min(year)) |>
5353
filter(year == max(year)) |>
5454
ungroup()
5555
@@ -102,5 +102,3 @@ ggplot(bar_data, aes(x = reorder(country, -pop), y = pop)) +
102102
caption = "Source: Gapminder"
103103
)
104104
```
105-
106-
**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

Comments
 (0)