Superscripts in legend title ggplot2

Here is a way to add a superscript to a legend title using the ggtext package.

library(tidyverse)
library(ggtext)

ggplot(mtcars, aes(x = disp, y = hp, color = hp, size = am)) +
  geom_point() +
  scale_color_continuous(name = 'Density (indiv m<sup>-3</sup>)') +
  scale_size_continuous(name = 'Density (indiv m<sup>-3</sup>)') +
  theme(legend.title = element_markdown())

Created on 2023-01-11 with reprex v2.0.2.9000

2 Likes