Search code examples
rggplot2facet

ggplot fails to put numbers in italic in facet title


I am trying to have some of my facets in italic text, at which point I noticed that my numbers do not become italic. I noticed the same behaviour in axis labels. Importantly, only partial parts of the labels or only some facets should be italic.

Is there any way to circumvent/fix this?

MWE:

library(ggplot2)
library(tibble)
tibble(
    a = c('a1~italic(a1)', 'Not~Italic'),
    x = c(1,1),
    y = c(1,1)
) %>% 
    ggplot(aes(x,y)) + 
    geom_point() +
    facet_grid(a~., labeller = label_parsed) + 
    theme(
        strip.text = element_text(size = 20)
    ) +
    xlab(expression(italic(Italic~part~of~label1)~not~italic~part~of~label1))

Created on 2022-03-02 by the reprex package (v2.0.0)


Solution

  • Update after clarification:

    library(ggplot2)
      library(tibble)
      tibble(
        a = c("italic('a1a1')", 'Not~Italic'),
        x = c(1,1),
        y = c(1,1)
      ) %>% 
        ggplot(aes(x,y)) + 
        geom_point() +
        facet_grid(a~., labeller = label_parsed) + 
        theme(
          strip.text = element_text(size = 20)
        ) +
        xlab(expression(italic(Italic~part~of~label1)~not~italic~part~of~label1))
    

    enter image description here

    First answer: Here is how we could do it using element_text():

    library(ggplot2)
    library(tibble)
    
    tibble(
      a = 'a1a1',
      x = 1,
      y = 1
    ) %>% 
      ggplot(aes(x,y)) + 
      geom_point() +
      facet_grid(a~.)+
      theme(
        strip.text.y = element_text(
          size = 12, face = "italic")
        )
    

    enter image description here