Search code examples
rggplot2ggtext

Overline in ggplot axis label


I'm trying to create some ggplot with overlined axis title. Using a reproducible example, my code is:

library(ggplot2)
library(ggtext)

data("iris")

ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
  geom_point() +
  xlab("<span style = 'text-decoration: overline;'>Sepal</span> <span style = 'color: #00e000;'>Length</span>") +
  theme(axis.title.x = element_markdown())

The result, however, doesn't work as expected (overline missing on "Sepal"):

image1


Solution

  • If you don't mind losing the color, you can do it using plotmath methods:

    library(ggplot2)
    ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
      geom_point() +
      xlab(expression(paste(over(,"Sepal"), atop(," Length"))))
    

    Created on 2022-06-07 by the reprex package (v2.0.1)