I have a necessary reason to use 'ggtext'
This is not the result I want,You can see that there is a big gap between '2-' and 'O'
library(ggtext)
library(tidyverse)
df <- tibble(
label = c(
'SO<sub>4</sub><sup>2-</sup>'
),
x = c(.5),
y = c(.5),
)
ggplot(df) +
aes(
x, y, label = label
) +
geom_richtext() +
xlim(0, 1)+
ylim(0, 1)
This is the result I want, but this is not achieved with ggtext's html tags sub and sup
I can't find any reference that suggests HTML or markdown support the "atop" notion (aka vertical alignment of superscript and subscript); some answers have even said "not possible", though I can't say with certainty that that's the case.
This is possible using the basic geom_label
and plotmath-expressions.
Since you said that you have reason to use ggtext
, is it possible to separate the labels with this feature?
df <- tibble(
label = c(
'SO<sub>4</sub><sup>2-</sup>',
"'SO'[4]^'2-'"
),
x = c(.5, .5),
y = c(.4, .6),
parse = c(FALSE, TRUE)
)
ggplot(df) +
aes( x, y, label = label ) +
geom_richtext(data = ~ filter(., !parse)) +
geom_label(data = ~ filter(., parse), parse=TRUE) +
xlim(0, 1) + ylim(0, 1)
If desired, you can add some space after the SO
to make it a little less cramped; for instance, "'SO '[4]^'2-'"
results in