Learn R Programming

animint2 (version 2025.9.16)

geom_label_aligned: Non-overlapping label boxes

Description

This geom creates boxes with labels that are aligned either vertically or horizontally, using quadratic programming to optimize their positions and avoid overlaps. The QP solver is applied after all showSelected filtering occurs, and operates as follows:

Usage

geom_label_aligned(
  mapping = NULL,
  data = NULL,
  stat = "identity",
  position = "identity",
  ...,
  label_r = 0.15,
  alignment = "vertical",
  min_distance = 0.1,
  background_rect = TRUE,
  na.rm = FALSE,
  show.legend = NA,
  inherit.aes = TRUE
)

Arguments

mapping

Set of aesthetic mappings created by aes or aes_. If specified and inherit.aes = TRUE (the default), it is combined with the default mapping at the top level of the plot. You must supply mapping if there is no plot mapping.

data

The data to be displayed in this layer. There are three options:

If NULL, the default, the data is inherited from the plot data as specified in the call to ggplot.

A data.frame, or other object, will override the plot data. All objects will be fortified to produce a data frame. See fortify for which variables will be created.

A function will be called with a single argument, the plot data. The return value must be a data.frame., and will be used as the layer data.

stat

The statistical transformation to use on the data for this layer, as a string.

position

Position adjustment, either as a string, or the result of a call to a position adjustment function.

...

other arguments passed on to layer. These are often aesthetics, used to set an aesthetic to a fixed value, like color = "red" or size = 3. They may also be parameters to the paired geom/stat.

label_r

Radius of rounded corners. Defaults to 0.15 lines.

alignment

One of "vertical" (QP on Y axis) or "horizontal" (QP on X axis)

min_distance

Minimum distance between boxes in pixels.

background_rect

Disables text background rect if set to FALSE.

na.rm

If FALSE (the default), removes missing values with a warning. If TRUE silently removes missing values.

show.legend

logical. Should this layer be included in the legends? NA, the default, includes if any aesthetics are mapped. FALSE never includes, and TRUE always includes.

inherit.aes

If FALSE, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions that define both data and aesthetics and shouldn't inherit behaviour from the default plot specification, e.g. borders.

Details

For vertical alignment (default): - QP optimizes Y positions while keeping X positions fixed - Constraints ensure boxes don't overlap vertically - Boxes are aligned along the vertical axis at their original X positions

For horizontal alignment: - QP optimizes X positions while keeping Y positions fixed - Constraints ensure boxes don't overlap horizontally - Boxes are aligned along the horizontal axis at their original Y positions

The QP solver minimizes the total squared distance from original positions while enforcing minimum spacing constraints between boxes.

Examples

Run this code
library(nlme)
data(BodyWeight, package = "nlme")
# Extracting the last point of each rat's trajectory
library(data.table)
label_data <- data.table(BodyWeight)[Time == max(Time)][order(weight)]
rfac=function(x)factor(paste(x), paste(label_data$Rat))
BodyWeight$rat=rfac(BodyWeight$Rat)
label_data$rat=rfac(label_data$Rat)
library(animint2)
viz <- animint(
  bodyPlot = ggplot() +
    theme_bw() +
    theme_animint(width=1000)+
    geom_line(aes(
      x = Time, y = weight,
      group = rat),
      clickSelects="rat",
      size=3,
      data = BodyWeight) +
    geom_line(aes(
      x = Time, y = weight,
      group = rat, key = rat, colour = rat),
      clickSelects="rat",
      data = BodyWeight) +
    geom_label_aligned(aes(
      x = Time + 1, y = weight,
      key = rat, label = rat, fill = rat),
      clickSelects="rat",
      hjust = 0,
      data = label_data) +
    facet_grid(~Diet, labeller=label_both) +
    ggtitle("rat body weight over time by diet") +
    xlab("Time (days)") +
    ylab("Body Weight (grams)"),
  duration=list(rat=1000)
)
viz

Run the code above in your browser using DataLab