Skip to contents

Shifts leaf label positions in a dendextend ggdend object so that labels are separated from the dendrogram branch tips. Use this function before passing the object to ggplot and pair it with a matching scale expansion so the offset labels remain visible.

Usage

offset_dendro_labels(ggd, proportion = 0.015)

Arguments

ggd

A ggdend object as returned by as.ggdend.

proportion

Numeric. The fraction of the dendrogram height to offset labels by. Default is 0.015 (1.5%).

Value

The modified ggdend object (invisibly), making the function pipeable.

Details

Note that theme_dendro automatically offsets leaf labels and adds matching y-scale expansion, so manual offset and scale calls are only needed when you want a different proportion or when not using theme_dendro().

In a vertical dendrogram the offset pushes labels downward (away from the branches). In a horizontal dendrogram, where dendextend applies coord_flip() + scale_y_reverse(), the same downward offset pushes labels rightward (away from the leaf tips). In both cases the visual effect is consistent spacing between branch endpoints and label text.

Matching scale expansion

After offsetting the labels, the y-scale must be expanded in the direction the labels moved so they are not clipped. Use a matching proportion value in the scale:

Vertical

scale_y_continuous(expand = expansion(mult = c(proportion, 0)))

Horizontal

scale_y_reverse(expand = expansion(mult = c(0, proportion)))

Author

Dustin Stoltz

Examples

if (FALSE) { # \dontrun{
library(ggplot2)
suppressPackageStartupMessages(library(dendextend))

hc <- hclust(dist(USArrests[1:10, ]))
dend <- as.dendrogram(hc) |> set("branches_k_color", k = 3)
ggd <- as.ggdend(dend)

# Horizontal dendrogram with label offset
ggd |> offset_dendro_labels() |>
  ggplot(horiz = TRUE) +
  theme_dendro() +
  scale_y_reverse(expand = expansion(mult = c(0, 0.015)))

# Vertical dendrogram with label offset
ggd |> offset_dendro_labels() |>
  ggplot(horiz = FALSE) +
  theme_dendro(horiz = FALSE) +
  scale_y_continuous(expand = expansion(mult = c(0.015, 0)))
} # }