Skip to contents

Two modes:

Mode 1 (no id_by)

Numbers the levels sequentially: "01.B cell", "02.CD4 T", … Useful for shortening long names on UMAP centroid labels while keeping the full name in the legend.

Mode 2 (with id_by)

Creates a group × id interaction label. Each unique value of id_by within a group gets its own sequential number: "Male_1", "Male_2", "Female_1", …

Usage

NumberLabels(
  x,
  id_by = NULL,
  position = c("before", "after"),
  sep = ".",
  pad_width = NULL
)

Arguments

x

A vector (factor, character, or numeric).

id_by

Optional vector of the same length as x. When supplied, enables Mode 2: one sequential number per unique id_by value within each level of x.

position

"before" (default) places the number before the label; "after" places it after.

sep

Separator between the number and the label. Default ".".

pad_width

Integer. Width for zero-padding. NULL (default) auto-detects based on the maximum number.

Value

A factor with relabeled levels.

Details

If x is not already a factor it is converted automatically (with a message). Run SetLevels() first if you need a specific level order.

Examples

if (FALSE) { # \dontrun{
# Mode 1: number long SingleR labels for UMAP
SeuratObj$SingleRAssignment <- NumberLabels(SeuratObj$SingleRAssignment)
# levels: "01.B cell", "02.CD4 T", ...

# Mode 2: one number per patient within each Sex group
SeuratObj$SexByPatient <- NumberLabels(SeuratObj$Sex, id_by = SeuratObj$patient)
# levels: "Female_1", "Female_2", "Male_1", "Male_2", ...

# Append number after, custom separator
NumberLabels(SeuratObj$CellType, position = "after", sep = " - ")
} # }