beh99
beh99

Reputation: 3

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

Upvotes: 0

Views: 2945

Answers (1)

Matt L.
Matt L.

Reputation: 2954

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)

Upvotes: 1

Related Questions