--- title: "Labelling points" output: rmarkdown::html_vignette: toc: TRUE vignette: > %\VignetteIndexEntry{Labelling points} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r, include = FALSE} knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.width = 4, fig.height = 4, fig.align = "center", out.width = '60%', dpi = 120, message = FALSE ) library(ckbplotr) ``` ## Introduction Adding labels and annotations to plots can be done with ggplot2 using `ggplot2::geom_text()` and `ggplot2::geom_label()` (though this requires careful positioning of the text using the data coordinates.) The [ggrepel package](https://ggrepel.slowkow.com/) provides geoms to avoid overlapping text labels. In this package `geom_text_move()` can be used to place text labels at fixed offsets from coordinate positions. This can be useful to label points on a plot with manual control over their positions. ## Positioning The initial position of the text is set using the `x` and `y` aesthetics. The text can then be offset from this point as follows. ### By angle Use the `offset_clock` aesthetic to set the direction of the label offset, specified as a clock-face position (0–12). The justification of the text is automatically set depending on its position. Use the `offset_r` aesthetic to set the distance of the offset. The units of this distance are based on the text size, so this distance scales with the `size` aesthetic. ```{r} data.frame(x = 1:3, y = 1:3, label = c("Point A", "Point B", "Point C"), c = c(3, 4.5, 9), d = c(0.5, 1, 2)) |> ggplot(aes(x = x, y = y)) + geom_text_move(aes(label = label, offset_clock = c, offset_r = d)) + geom_point() + ckb_style(xlims = c(0, 4), ylims = c(0, 4), clip = "off") ``` Note that the position of the text labels relative to the points is invariant to changes in aspect ratio or the coordinate axes. ```{r} data.frame(x = 1:3, y = 1:3, label = c("Point A", "Point B", "Point C"), c = c(3, 4.5, 9), d = c(0.5, 1, 2)) |> ggplot(aes(x = x, y = y)) + geom_text_move(aes(label = label, offset_clock = c, offset_r = d)) + geom_point() + ckb_style(xlims = c(0, 5), ylims = c(0, 6), clip = "off", ratio = 0.5) ``` ### By offset For more manual control, use `offset_x`/`offset_y` to set the x/y offset and `hjust`/`vjust` to control the horizontal/vertical justification of the text (compared to the offset position). ```{r} data.frame(x = 1:3, y = 1:3, label = c("Point A", "Point B", "Point C"), c = c(0.5, 1, -1), d = c(0, 1, -1), h = c(0, 0, 1), v = c(0.5, 1, 0.5) ) |> ggplot(aes(x = x, y = y)) + geom_text_move(aes(label = label, offset_x = c, offset_y = d, hjust = h, vjust = v)) + geom_point() + ckb_style(xlims = c(0, 4), ylims = c(0, 4), clip = "off") ``` ## Lines Use the `line` aesthetic to add line segments connecting text labels to data points. ```{r} data.frame(x = 1:3, y = 1:3, label = c("Point A", "Point B", "Point C"), c = c(3, 4.5, 9), d = c(0.5, 1, 2), l = c(0, 1, 1)) |> ggplot(aes(x = x, y = y)) + geom_text_move(aes(label = label, offset_clock = c, offset_r = d, line = l)) + geom_point() + ckb_style(xlims = c(0, 4), ylims = c(0, 4), clip = "off") ``` Use `linecolour` to control the colour of line segments and `clip0`/`clip1` to control clipping of the line segments at the data/text ends. The `arrow` argument can be used to add arrow heads. ```{r} data.frame(x = 1:3, y = 1:3, label = c("Point A", "Point B", "Point C"), c = c(3, 4.5, 9), d = c(0.5, 2, 2), l = c(0, 1, 1)) |> ggplot(aes(x = x, y = y)) + geom_text_move(aes(label = label, offset_clock = c, offset_r = d, line = l), linecolour = "blue4", clip0 = 2, arrow = arrow(length = unit(1, "mm"), ends = "first", type = "closed")) + geom_point() + ckb_style(xlims = c(0, 4), ylims = c(0, 4), clip = "off") ``` ## Markdown Set `marquee = TRUE` to create the text labels using `marquee::element_marquee()` which allows some markdown and styling. ```{r} data.frame(x = 1:3, y = 1:3, label = c("**Point** *A*", "{.red Point} B", "Point{.sub C}"), c = c(3, 4.5, 9), d = c(0.5, 1, 2), l = c(0, 1, 1)) |> ggplot(aes(x = x, y = y)) + geom_text_move(aes(label = label, offset_clock = c, offset_r = d, line = l), marquee = TRUE) + geom_point() + ckb_style(xlims = c(0, 4), ylims = c(0, 4), clip = "off") ```