wilkelab / ggtext

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

element_markdown() does not work when facet_wrap(strip.position = "left") #63

Closed graco-roza closed 3 years ago

graco-roza commented 3 years ago

I have read all the discussion on the use of HTML code with {glue}, {ggtext} and {ggplot2} in the facet titles using ggplot and element_markdown(), then I manage to use the example given by @clauswilke to generate the plot

library(tidyverse)
library(glue)
#> 
#> Attaching package: 'glue'
#> The following object is masked from 'package:dplyr':
#> 
#>     collapse
library(ggtext)

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()

Rplot

However, when applying strip.position = "left" or even switch = "y" in the facet function, the plot does not format the HTML code.

library(tidyverse)
library(glue)
#> 
#> Attaching package: 'glue'
#> The following object is masked from 'package:dplyr':
#> 
#>     collapse
library(ggtext)

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, strip.position = "left") +
  theme(
    strip.text = element_markdown(),
    strip.text.x = element_markdown(),
    strip.text.y = element_markdown()

error-Rplot01

I searched for similar issues here but I couldn't find any. Also, if this is something related to the wrong use of functions I apologize in advance.

clauswilke commented 3 years ago

See here: https://github.com/wilkelab/ggtext/issues/5#issuecomment-539155038 Same problem.