Search code examples
rggplot2

Italicize a part of a ggplot title while displaying it on two lines


To italicize a few words of my ggplot title, I found the package mdthemes pretty useful. However, the title are sometimes too long to fit on one line only, so that it requires to be splitted on two. I usually do so by using \n where I want it to be splitted, but it doesn't work while using markdown. Is there any other way to separate the title ? Or would it be more manageable with another method than mdthemes ? Note : I also tried with str_wrap() from the package stringr, but it also didn't work out.

Here is the code I used.

library(ggplot2)
library(mdthemes)

data(iris)
p <- ggplot(iris, aes(x=Species, y=Sepal.Length)) +     
  geom_boxplot(show.legend=FALSE, outlier.shape = NA, aes(fill = Species), alpha = 0.8) +               
  labs(title ="Sepal length (cm) of *setosa, versicolor, virginica* \n exposed to different treatments") +
  theme_classic() +                                     
  theme(axis.title = element_text(face="bold"), 
        text = element_text(family = "serif", size = 12))
p +  md_theme_classic() 

enter image description here


Solution

  • Try adding a html line break <br> instead of \n, e.g.

    library(ggplot2)
    # install.packages("mdthemes")
    library(mdthemes)
    
    ggplot(iris, aes(x=Species, y=Sepal.Length)) +     
      geom_boxplot(show.legend=FALSE, outlier.shape = NA, aes(fill = Species), alpha = 0.8) +               
      labs(title = "Sepal length (cm) of *setosa*, *versicolor*, *virginica* <br> exposed to different treatments") +
      theme(axis.title = element_text(face="bold"), 
            text = element_text(family = "serif", size = 12)) +
      md_theme_classic()
    

    Created on 2023-11-27 with reprex v2.0.2


    Also, I use Claus Wilke's excellent ggtext package for this type of thing and I highly recommend it for anything 'complicated', e.g.

    library(ggplot2)
    library(ggtext)
    
    ggplot(iris, aes(x=Species, y=Sepal.Length)) +     
      geom_boxplot(show.legend=FALSE, outlier.shape = NA, aes(fill = Species), alpha = 0.8) +               
      labs(title = "Sepal length (cm) of *setosa*, *versicolor*, *virginica* <br> exposed to different treatments") +
      theme_classic() +                                     
      theme(plot.title = element_markdown(),
            axis.title = element_text(face="bold"), 
            text = element_text(family = "serif", size = 12))
    

    Created on 2023-11-27 with reprex v2.0.2


    Further examples of customisation options:

    library(ggplot2)
    library(ggtext)
    
    ggplot(iris, aes(x=Species, y=Sepal.Length)) +     
      geom_boxplot(show.legend=FALSE, outlier.shape = NA, aes(fill = Species), alpha = 0.8) +               
      labs(title = "Sepal length (cm) of *setosa*, *versicolor*, *virginica* <br> exposed to different treatments") +
      theme(axis.title = element_text(face="bold"), 
            text = element_text(family = "serif", size = 12),
            plot.title = element_markdown(halign = 0.5))
    

    
    ggplot(iris, aes(x=Species, y=Sepal.Length)) +     
      geom_boxplot(show.legend=FALSE, outlier.shape = NA, aes(fill = Species), alpha = 0.8) +               
      labs(title = "Sepal length (cm) of *setosa*, *versicolor*, *virginica* <br> exposed to different treatments") +
      theme(axis.title = element_text(face="bold"), 
            text = element_text(family = "serif", size = 12),
            plot.title = element_markdown(hjust = 0.5, halign = 0))
    

    
    color_palette <- scales::hue_pal()(3)
    color_palette
    #> [1] "#F8766D" "#00BA38" "#619CFF"
    ggplot(iris, aes(x=Species, y=Sepal.Length)) +     
      geom_boxplot(show.legend=FALSE, outlier.shape = NA, aes(fill = Species), alpha = 0.8) +               
      labs(title = "Sepal length (cm) of
           <span style='color:#F8766D'>*setosa*</span>,
           <span style='color:#00BA38'>*versicolor*</span>,
           <span style='color:#619CFF'>*virginica*</span><br>
           exposed to different treatments") +
      theme(axis.title = element_text(face="bold"), 
            text = element_text(family = "serif", size = 12),
            plot.title = element_markdown(hjust = 0.5, size = 20))
    

    Created on 2023-11-28 with reprex v2.0.2