| 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 |
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.
labs_map(names)labs_map(names)
names |
A named list or vector where names are variable names and values are the desired labels for those variables in the plot. |
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.
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.
labs for manual label setting
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)" ))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)" ))
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.
labs_nn(dict = "default")labs_nn(dict = "default")
dict |
The dictionary name to use for nickname lookups (optional). |
This makes it easy to specify nice names once and use them across a project.
labs_map for direct remapping
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()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()
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.
nn_add_labels(df, dict = "default")nn_add_labels(df, dict = "default")
df |
A dataframe to add labels to |
dict |
The dictionary name to use for nickname lookups (defaults to "default") |
The dataframe with label attributes added to matching columns
# 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"# 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"
Register and look up nickname mappings.
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")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")
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. |
nn_register() returns nothing.
nn() returns the nickname if one is registered, otherwise the original value.
# 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)# 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)