hlab
hlab.RdEasy Extraction of Labels/Units Expressions for Plotting
Arguments
- x
a single variable name, unquoted
- name
a single character string providing an alternate way to name
xthat is useful whenhlabis called from another function such ashlabs- html
set to
TRUEto return HTML strings instead ofplotmathexpressions- plotmath
set to
FALSEto use plain text instead of plotmath
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.
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)
#> expression(x)
hlab(x, html=TRUE)
#> [1] "x"
hlab(z)
#> expression(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)
#> expression(x)
# Remove LabelsUnits and use a current dataset named
# d2 instead of the default d
rm(LabelsUnits)
options(current_ds='d2')