1

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

msfrn
  • 57
  • 3
  • 3
    From the [Github readme](https://github.com/wilkelab/ggtext): "It currently can make text bold or italics, can change the font, color, or size of a piece of text, can place text as sub- or superscript, and has extremely rudimentary image support. No other features are currently supported." Overlines a probably not supported. – teunbrand Jun 07 '22 at 12:29
  • 1
    `ggtext` works via the `gridtext` package, which implements some elements of markdown and css by translating them into `grid` graphics. `gridtext` does its own parsing and translation to grid, using a small but very commonly used subset of tags and css attributes, rather than providing a full markdown and css implementation. It does not handle overlines, as @teunbrand points out. – Allan Cameron Jun 07 '22 at 12:41

2 Answers2

3

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)

user2554330
  • 37,248
  • 4
  • 43
  • 90
2

As mentioned in the comments, ggtext does not handle overlines. Perhaps the simplest way to get the same effect without having to turn clipping off and draw line segments is to use two-line text with underscores on the top line:

library(ggplot2)
library(ggtext)

data("iris")

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

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87