ch_add_stacked_labels: Add stacked labels to the right of a chart

View source: R/ch_add_stacked_labels.r

ch_add_stacked_labelsR Documentation

Add stacked labels to the right of a chart

Description

This function adds ggrepel::geom_label_repel() to a chart. It requires you to specify the label aesthetic in the original ggplot. It also extends the x-axis by a certain amount (20\ of the arguments get passed directly to ggrepel::geom_label_repel(). This expansion will combine with any other expansion present in the plot, such as arguments based to ggplot2::scale_x_continuous(). It needs to be used with a %>% and not a +.

Usage

ch_add_stacked_labels(
  .plot,
  pct_extend = 1.2,
  nudge_x = NULL,
  segment.colour = NA,
  min.segment.length = 0.1,
  max.overlaps = 20,
  segment.alpha = 0.5,
  segment.size = 0.1,
  hjust = "right",
  direction = "y",
  size = 3,
  ...
)

Arguments

.plot

(a ggplot2 object) with the label aesthetic specified.

pct_extend

(numeric, default = 1.2) how much to expand the x-axis. The existing x-axis range will be multiplied by this value, so the default expands by 20%.

nudge_x

(numeric; default = NULL) Horizontal and vertical adjustments to nudge the starting position of each text label.

segment.colour

(character; default = NA) colour of the line segment, NA means no line segment

min.segment.length

Skip drawing segments shorter than this, as unit or number. Defaults to 0.5. (Default unit is lines, but other units can be specified by passing unit(x, "units")).

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.

segment.alpha

(number; default = 0.5) the transparency of the line segment

segment.size

(number; default = 0.1) the size of the line segment

hjust

(default = "right") the horizontal justification of the label

direction

"both", "x", or "y" – direction in which to adjust position of labels

size

(number, default = 3) the size of the text in the label

...

other arguments passed to geom_label_repel

Details

If you get a warning about Aesthetics not being the right length for size, that's a cryptic message telling you to set the size argument. Setting it to 3 usually looks alright.

Value

a ggplot2 object

Examples

## Not run: 
some_plot |> ch_add_stacked_labels()

## End(Not run)


baslat/sak documentation built on April 14, 2025, 4:14 p.m.