wilkelab / ggtext

Improved text rendering support for ggplot2
https://wilkelab.org/ggtext/
GNU General Public License v2.0
655 stars 37 forks source link

Markdown/HTML in facet labels #2

Closed hrbrmstr closed 5 years ago

hrbrmstr commented 5 years ago
library(glue)
library(ggtext)
library(ggplot2)

count(mtcars, cyl, carb) %>% 
  mutate(cyl_lab = as.character(
    glue("Cylinders {cyl} <span style='color:red'>(n={n})</span>")
  )) %>% 
  ggplot(aes(carb, n)) +
  geom_col() +
  facet_wrap(~cyl_lab) +
  theme(
    strip.text = element_markdown(),
    strip.text.x = element_markdown(),
    strip.text.y = element_markdown()
  )

image

I'll poke at why this isn't working this weekend.

clauswilke commented 5 years ago

Probably the faceting code doesn't use element_render()/element_grob() to generate the labels.

hrbrmstr commented 5 years ago

Heh. It does (in ggstrip()), but after it pre-renders the strip titles with create_strip_labels() which uses title_spec() and bypasses the dispatch you hook in to.

I'll poke a bit more then post an issue on {ggplot2}.