hlab.Rd
Easy Extraction of Labels/Units Expressions for Plotting
hlab(x, name = NULL, html = FALSE, plotmath = TRUE)
a single variable name, unquoted
a single character string providing an alternate way to name x
that is useful when hlab
is called from another function such as hlabs
set to TRUE
to return HTML strings instead of plotmath
expressions
set to FALSE
to use plain text instead of plotmath
an expression created by labelPlotmath
with plotmath=TRUE
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.
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')