The point of this exercise is to demonstrate flexible rendering of subscripts and superscripts. We want to write expressions for column labels and units that are fairly readable as they are, and yet can be easily rendered with equivalent results in plotmath, html, or pdf.
First we load some packages.
##
## Attaching package: 'yamlet'
## The following object is masked from 'package:stats':
##
## filter
We create some toy data.
x <- data.frame(time = 1:10, work = (1:10)^1.5)
x %<>% decorate('
time: [ Time_cum.^alpha, h ]
work: [ Work_total_obs, kg*m^2/s^2 ]
')
x %>% decorations
## - time
## - label: Time_cum.^alpha
## - guide: h
## - work
## - label: Work_total_obs
## - guide: kg*m^2/s^2
Notice that the label for column work
has nested
subscripts: obs is subordinate to total, and the
result is subordinate to Work.
For the column time
, the word Time is followed
by a subscript cum. Then, cum is followed by the
superscript alpha. In this case alpha intends to be
the superscript of Time_cum not just cum. We include
the dot to explicitly terminate the cum subscript, which causes
alpha to be the superscript of the combined result.
How does this look when we plot it?
By default, we get verbatim labels and units as substitutes for column names.
Next, we use scripted()
instead of
resolve()
to indicate that the labels should be understood
as potentially having subscripts and superscripts. For this to work
well, units should be constructed using *, /, and ^ (even though the
“units” package supports other encodings).
In the background, scripted()
is writing
expression attributes (consumed by
ggplot()
) and title attributes (consumed
by tablet()
). We illustrate the latter.
All (N = 10) |
|
---|---|
Timecumα (h) | |
Mean (SD) | 5.5 (3.03) |
Median (range) | 5.5 (1, 10) |
Worktotalobs (kg·m2/s2) | |
Mean (SD) | 14.3 (10.5) |
Median (range) | 12.9 (1, 31.6) |
In summary, we have decorated our data with labels and units containing markup for subscripts and superscripts. If everything goes well, these render similarly in figures and tables. They also render similarly in html and pdf. Please see the pdf version of this document.
```