Converts a character or numeric vector into a factor where the levels are
ordered naturally (e.g., a1, a2, ..., a10 instead of
lexicographically as a1, a10, a2, ...).
This is useful for plotting or labeling grouped data where numeric
substrings should follow numeric order.
Examples
# Numeric vector
factorNaturalOrder(10:1)
#> [1] 10 9 8 7 6 5 4 3 2 1
#> Levels: 1 2 3 4 5 6 7 8 9 10
# Character vector with embedded numbers
factorNaturalOrder(c("a11", "a12", "a1", "a2", "a"))
#> [1] a11 a12 a1 a2 a
#> Levels: a a1 a2 a11 a12