View source: R/add-annotation.R
add_data_labels | R Documentation |
Add data labels
add_data_labels(
plot,
label,
data = all_rows(),
fontsize = 7,
background = FALSE,
background_color = "#FFFFFF",
background_alpha = 0.6,
label_position = c("below", "above", "left", "right", "center"),
...
)
add_data_labels_repel(
plot,
label,
data = all_rows(),
fontsize = 7,
segment.size = 0.2,
box.padding = 0.2,
max.overlaps = Inf,
background = FALSE,
background_color = "#FFFFFF",
background_alpha = 0.6,
...
)
plot |
A |
label |
Variable in the dataset to be used for the text label. |
data |
The data to be displayed in this layer. There are three options:
|
fontsize |
Font size in points. Defaults to |
background |
Whether to include semitransparent background box behind the labels to improve legibility. Defaults to |
background_color |
Hex color of the background box. Defaults to |
background_alpha |
Opacity of the background box. Defaults to |
label_position |
Position of the label in relation to the data point. Can be one of |
... |
Arguments passed on to the |
segment.size |
Thickness of the line connecting the label with the data point. Defaults to |
box.padding |
Amount of padding around bounding box, as unit or number.
Defaults to 0.25. (Default unit is lines, but other units can be specified
by passing |
max.overlaps |
Exclude text labels when they overlap too many other things. For each text label, we count how many other text labels or other data points it overlaps, and exclude the text label if it has too many overlaps. Defaults to 10. |
add_data_labels_repel()
uses ggrepel::geom_text_repel()
. Check there
and in ggrepel examples for
additional arguments.
add_data_labels()
and add_data_labels_repel()
support data subsetting. See Advanced plotting.
A tidyplot
object.
# Create plot and increase padding to make more space for labels
p <-
animals |>
dplyr::slice_head(n = 5) |>
tidyplot(x = weight, y = speed) |>
theme_ggplot2() |>
add_data_points() |>
adjust_padding(all = 0.3)
# Default label position is `below` the data point
p |> add_data_labels(label = animal)
# Alternative label positions
p |> add_data_labels(label = animal, label_position = "above")
p |> add_data_labels(label = animal, label_position = "right")
p |> add_data_labels(label = animal, label_position = "left")
# Include white background box
p |> add_data_labels(label = animal, background = TRUE)
p |> add_data_labels(label = animal, background = TRUE,
background_color = "pink")
# Black labels
p |> add_data_labels(label = animal, color = "black")
# Use repelling data labels
p |> add_data_labels_repel(label = animal, color = "black")
p |> add_data_labels_repel(label = animal, color = "black",
background = TRUE)
p |> add_data_labels_repel(label = animal, color = "black",
background = TRUE, min.segment.length = 0)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.