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.
knitr::opts_chunk$set(dpi = 600, out.width = '50%')
library(magrittr) library(ggplot2) library(tablet) library(yamlet) library(dplyr) library(kableExtra)
We create some example data.
x <- data.frame( time = 1:10, work = (1:10)^1.5, group = 1:2, set = c(rep('delta',5), rep('gamma', 5)) ) x %<>% decorate(' time: [ Time_cum.^alpha, h ] work: [ Work_total_obs\\n, kg*m^2/s^2 ] group: [ Group, [ Second\\nGroup^\\*: 2, First\\nGroup^#: 1 ]] set: [ Set, [ gamma, delta ]] ') x %>% decorations
The label for column work
has nested subscripts suggesting
$\sf{Work_{total_{obs}}}$. The label for column time
suggests
$\sf{Time_{cum}{}^{\alpha}}$. The dot closes the subscript to distinguish
this from $\sf{Time_{cum^{\alpha}}}$. Backslash-n requests a line break.
How does this look when we plot it?
x %>% resolve %>% ggplot(aes(time, work, color = group, shape = set)) + geom_point()
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).
x %>% scripted %>% ggplot(aes(time, work, color = group, shape = set)) + geom_point()
In the background, scripted()
is writing expression and plotmath attributes
(consumed by ggplot()
) and title attributes (consumed by tablet()
).
We illustrate the latter.
x %>% scripted %>% group_by(group, set) %>% tablet %>% as_kable
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 html version of this document.
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.