0

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

beh99
  • 3
  • 2
  • Do any of the following help? https://stackoverflow.com/q/17334759/3358272 and https://stackoverflow.com/q/27445452/3358272. (Searching SO and google in general can be hard for R, these two were found with [`[r] ggplot2 label subscript`](https://stackoverflow.com/search?q=%5Br%5D+ggplot2%20label%20subscript), fyi.) – r2evans Apr 29 '21 at 15:31
  • Does this answer your question? [Subscript letters in ggplot axis label](https://stackoverflow.com/questions/17334759/subscript-letters-in-ggplot-axis-label) – tjebo Apr 29 '21 at 18:07

1 Answers1

1

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)

Matt L.
  • 2,753
  • 13
  • 22