Is it possible to use ggtext to plot symbols on top of each other?

108 Views Asked by At

I try to use geom_richtext of the "ggtext" package to create my labels and I want to plot a number and a letter on top of each other. But I don't know whether this is even possible with "ggtext" yet. I could only achieve the desired layout with the use of a complicated combination of expression, atop, and paste. Nonetheless, I would strongly prefer an HTML solution, if there is one. Any ideas?

Here is what I was able to code so far:

library(tibble)
library(ggplot2)
library(ggtext)

dummy <- tibble(this = "A", that = 1)

my_label1 <- "*p* = 0.05<br>&omega;<sub>*p*</sub><sup>2</sup>: large"

dummy %>%
  ggplot(., aes(this, that)) +
  theme_void() +
  geom_richtext(
    aes(0, 0, label = my_label1),
    size = 10, label.colour = "grey", hjust = 0
    )
#> Warning in text_info(label, fontkey, fontfamily, font, fontsize, cache): unable
#> to translate '<U+03C9>png128.4527559055118' to native encoding
#> Warning in text_info_cache[[key]] <- info: unable to translate
#> '<U+03C9>png128.4527559055118' to native encoding


my_label2 <- expression(atop(paste(italic("p")," = 0.05 "), paste(omega [italic("p")] ^"2", ": large")))

dummy %>%
  ggplot(., aes(this, that)) +
  theme_void() +
  geom_label(
    aes(0, 0),
    label = my_label2, parse = T, size = 10
    )
#> Warning in is.na(x): is.na() auf Nicht-(Liste oder Vektor) des Typs 'expression'
#> angewendet

Created on 2022-11-17 with reprex v2.0.2

0

There are 0 best solutions below