I am trying to make a table using library(gt)
. How would I make a column label include superscript or subscript? I have tried using expression
and square brackets [] or _ etc, but it isn't doing the desired output. I would like a column name of "[H2O] (mg/mL)". Where the 2 is subscript or superscript.
data_table <- iris %>%
group_by(Species) %>%
summarise(Mean = mean(Petal.Width), SD = sd(Petal.Width), n = n()) %>%
ungroup() %>%
gt()
data_table %>% cols_label(
Mean = expression('[H'[2]'O] (mg/ml)')
)
Similarly, when using ggplot, when trying to make the legend using:
labs(colour = "[H2O] (mg/ml)")
I would like the 2 to be subscript. I have tried bquote and expression here too and does not work due to the colour assignment.
CodePudding user response:
Using gt::html
and a <sub>
tag (or <sup>
for superscripts) you could do:
library(dplyr)
library(gt)
data_table <- iris %>%
group_by(Species) %>%
summarise(Mean = mean(Petal.Width), SD = sd(Petal.Width), n = n()) %>%
ungroup() %>%
gt()
data_table %>%
cols_label(
Mean = html('H<sub>2</sub>O (mg/ml)')
)