Search code examples
rggplot2graphhistogramaxis-labels

Using subscript and italics in ggplot2 axis labels


I am using ggplot2 to create a histogram, but have been struggling to format the axis label. I have so far used to code below to insert a greek letter in the label, but would also like the 'K' to be in italics and the 'D' to be subscript so that the label looks like K D (µM)

labs(x=expression(paste('KD (', mu, 'M)')))


Solution

  • The easiest way to format without using the expression method is to use simple html. I recommend the ggtext package

    see also this answer regarding Greek text in ggplot2

    library(tidyverse)
    library(ggtext) # 
    mtcars %>% 
      ggplot() +
      geom_histogram(aes(drat), bins = 20) +
      labs(x="K<sub><i>D</i></sub>(\u00b5M)") +
      theme(axis.title.x = element_markdown())
    

    Created on 2021-04-29 by the reprex package (v2.0.0)