Learn R Programming

Hmisc (version 5.1-3)

hlab: hlab

Description

Easy Extraction of Labels/Units Expressions for Plotting

Usage

hlab(x, name = NULL, html = FALSE, plotmath = TRUE)

Value

an expression created by labelPlotmath with plotmath=TRUE

Arguments

x

a single variable name, unquoted

name

a single character string providing an alternate way to name x that is useful when hlab is called from another function such as hlabs

html

set to TRUE to return HTML strings instead of plotmath expressions

plotmath

set to FALSE to use plain text instead of plotmath

Author

Frank Harrell

Details

Given a single unquoted variable, first looks to see if a non-NULL LabelsUnits object exists (produced by extractlabs()). When LabelsUnits does not exist or is NULL, looks up the attributes in the current dataset, which defaults to d or may be specified by options(current_ds='name of the data frame/table'). Finally the existence of a variable of the given name in the global environment is checked. When a variable is not found in any of these three sources or has a blank label and units, an expression() with the variable name alone is returned. If html=TRUE, HTML strings are constructed instead, suitable for plotly graphics.

The result is useful for xlab and ylab in base plotting functions or in ggplot2, along with being useful for labs in ggplot2. See example.

See Also

label(), units(), contents(), hlabs(), extractlabs(), plotmath

Examples

Run this code
d <- data.frame(x=1:10, y=(1:10)/10)
d <- upData(d, labels=c(x='X', y='Y'), units=c(x='mmHg'), print=FALSE)
hlab(x)
hlab(x, html=TRUE)
hlab(z)
require(ggplot2)
ggplot(d, aes(x, y)) + geom_point() + labs(x=hlab(x), y=hlab(y))
# Can use xlab(hlab(x)) + ylab(hlab(y)) also
# Store names, labels, units for all variables in d in object
LabelsUnits <- extractlabs(d)
# Remove d; labels/units still found
rm(d)
hlab(x)
# Remove LabelsUnits and use a current dataset named
# d2 instead of the default d
rm(LabelsUnits)
options(current_ds='d2')

Run the code above in your browser using DataLab