Package 'nicknames'

Title: Apply Human Readable Names to Plot Axes, Dataframe Columns and Anywhere Else
Description: Nicknames allows you to specify human readable names for the columns in your data once and then reuse them across your project to rename plots axes, dataframe columns, tables and anything else.
Authors: Jan Simson [aut, cre] (ORCID: <https://orcid.org/0000-0002-9406-7761>)
Maintainer: Jan Simson <[email protected]>
License: MIT + file LICENSE
Version: 0.0.0.9000
Built: 2026-06-01 08:46:57 UTC
Source: https://github.com/jansim/nicknames

Help Index


Map Names to Aesthetic Labels in ggplot2

Description

This function automatically maps variable names to labels based on a plot's aesthetic mapping. It provides a convenient way to set multiple labels at once by matching the names provided to aes / used by default in ggplot with a prespecified list.

Usage

labs_map(names)

Arguments

names

A named list or vector where names are variable names and values are the desired labels for those variables in the plot.

Details

This is particularly useful when you have a consistent naming scheme for variables and want to apply human-readable labels without manually specifying each aesthetic.

Value

An object of class "labs_map" that can be added to a ggplot object using the + operator. When added, it will automatically apply appropriate labels based on the plot's aesthetic mappings.

See Also

labs for manual label setting

Examples

library(ggplot2)

# Create plot and apply labels
ggplot(mtcars, aes(x = mpg, y = hp, color = factor(cyl))) +
  geom_point() +
  labs_map(c(
    "mpg" = "Miles per Gallon",
    "hp" = "Horsepower",
    "cyl" = "Number of\nCylinders"
  ))

# Even though names (e.g. cyl) are extracted, exact matches take priority
ggplot(mtcars, aes(x = mpg, y = hp, color = factor(cyl))) +
  geom_point() +
  labs_map(c(
    "mpg" = "Miles per Gallon",
    "hp" = "Horsepower",
    "cyl" = "Number of\nCylinders",
    "factor(cyl)" = "Number of\nCylinders (factor)"
  ))

Use Nicknames in ggplot2 Plots

Description

This function provides a convenient way to apply human readable labels to ggplot2 plots, by first registering them using nn_register and then applying them using this function.

Usage

labs_nn(dict = "default")

Arguments

dict

The dictionary name to use for nickname lookups (optional).

Details

This makes it easy to specify nice names once and use them across a project.

See Also

labs_map for direct remapping

Examples

library(ggplot2)

# Register nicknames
nn_register(c(
  "mpg" = "Miles per Gallon",
  "hp" = "Horsepower",
  "factor(cyl)" = "Number of\nCylinders"
))

# Create plot and apply nickname labels
ggplot(mtcars, aes(x = mpg, y = hp, color = factor(cyl))) +
  geom_point() +
  labs_nn()

Add Labels to Dataframe Columns via Nicknames

Description

This function takes a dataframe and adds labels to columns that have registered nicknames. The labels are stored as the "label" attribute for each matching column and are visible in e.g. the Rstudio data viewer.

Usage

nn_add_labels(df, dict = "default")

Arguments

df

A dataframe to add labels to

dict

The dictionary name to use for nickname lookups (defaults to "default")

Value

The dataframe with label attributes added to matching columns

Examples

# Register some nickname mappings
nn_register(c(
  "mpg" = "Miles per Gallon",
  "hp" = "Horsepower",
  "cyl" = "Number of Cylinders"
))

# Add labels to mtcars dataframe
labeled_mtcars <- nn_add_labels(mtcars)

# If you're using Rstudio, run View(labeled_mtcars)
# or check the labels manually:
attr(labeled_mtcars$mpg, "label") # "Miles per Gallon"
attr(labeled_mtcars$hp, "label") # "Horsepower"

Nickname registration and lookup

Description

Register and look up nickname mappings.

Usage

nn_register(mappings, dict = "default")

nn(x, dict = "default")

## Default S3 method:
nn(x, dict = "default")

## S3 method for class 'data.frame'
nn(x, dict = "default")

Arguments

mappings

A named vector where names are original values and values are nicknames

dict

The dictionary name to use (defaults to "default").

x

The value to look up, this can be a dataframe or character vector.

Value

nn_register() returns nothing. nn() returns the nickname if one is registered, otherwise the original value.

Examples

# Register some nickname mappings
nn_register(c(
  "Jennifer" = "Jen",
  "Robert" = "Bob",
  "Elizabeth" = "Liz"
))

# Look up nicknames
nn("Jennifer")  # Returns "Jen"
nn("Robert")    # Returns "Bob"
nn("John")      # Returns "John" (no mapping registered)

# Use different dictionaries
nn_register(c("Jennifer" = "Jenny"), dict = "alt")
nn("Jennifer")              # Returns "Jen" (from default dict)
nn("Jennifer", dict = "alt") # Returns "Jenny" (from alt dict)