Easy Extraction of Labels/Units Expressions for Plotting
hlab(x, name =NULL, html =FALSE, 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
Returns
an expression created by labelPlotmath with plotmath=TRUE
Details
Given a single unquoted variable, first looks to see if a non-NULLLabelsUnits 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.
Examples
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 objectLabelsUnits <- extractlabs(d)# Remove d; labels/units still foundrm(d)hlab(x)# Remove LabelsUnits and use a current dataset named# d2 instead of the default drm(LabelsUnits)options(current_ds='d2')