shorten_legend: Shorten legend names to a given length

View source: R/shorten_legend.R

shorten_legendR Documentation

Shorten legend names to a given length

Description

1. If ylab is specified, the function just returns everything after the ylab. 2. If identical_only = TRUE is specified, it removes identical parts in the name, independent of maxchar. 3. If maxchar is specified, the function will first try to return vector as it is, then remove elements which are identical between all elements and finally cut the end of the character vectors so that it fits the given maxchar setting. Underscores will be replaced with empty spaces for further processing

Usage

shorten_legend(
  x,
  maxchar = 20,
  identical_only = FALSE,
  ylab = NULL,
  sep = c(" ", "|", "_"),
  unit = NULL
)

Arguments

x

A character vector or a factor vector that should be shortened

maxchar

Maximum number of characters allowed

identical_only

If set to TRUE identical parts in the name will be removed, independent of the character length (maxchar will be ignored!)

ylab

If specified this part will be removed, independent of maxchar and identical_only

sep

A vector of characters which should be interpreted as separators

unit

A vector of characters with units, pasted to ylab

Author(s)

Jan Philipp Dietrich, Oliver Richters

Examples

a <- c("Model Scenario_BLUB", "Model Scenario_BLA", "Model Scenario_BLA_BLA_BLUB")

# do nothing
shorten_legend(a, 30)
# remove identical parts
shorten_legend(a, 15)
# or ...
shorten_legend(a, identical_only=TRUE)
# cutoff end of string
shorten_legend(a, 5)
# cut off the first part as explicitly specified
shorten_legend(a, ylab = "Model Scenario")

pik-piam/mip documentation built on April 13, 2025, 5:45 p.m.