Plot title containing math $\\times$ sign

Viewed 667

I would like to include the mathematical sign for a multiplication (like the lower-case x) in a plot title.

I have tried using latex2exp to no avail - can someone help me with this?

The reprex code is this:

library(ggplot2)
library(latex2exp)
ggplot(data = data.frame(number = round(rnorm(200, mean=55, sd=5))),
       aes(x = number)) + geom_density() +
  ggtitle(TeX("Title containing times sign here: $\\times$"))

And it produces this outcome:

enter image description here

I would like the square (indicating an unrecognized sign) to display as this sign: enter image description here. It seems that other signs work (e.g. alpha) using this approach but I cannot figure out why the times sign poses an issue.

I'm not set on using latex2exp for the solution, as long as it works with the latex font (LM Roman 10).

Thanks a lot in advance.

2 Answers

One approach might be to use the unicode code for the multiplication symbol:

library(ggplot2)
ggplot(data = data.frame(number = round(rnorm(200, mean=55, sd=5))),
       aes(x = number)) + geom_density() +
  ggtitle("Title containing times sign here: \u00D7")

Unicode characters can be added to any string with \u and the 4 hex code. You can find the appropriate code from your OS or with Google.

It works for me with plotmath facilities: the %*% operator displays as the times symbol. For example,

library(ggplot2)
ggplot(data = data.frame(number = round(rnorm(200, mean=55, sd=5))),
       aes(x = number)) + geom_density() +
  ggtitle(expression("Title containing times sign here: " %*% ""))

Created on 2021-06-14 by the reprex package (v2.0.0)

I think that is what latex2exp was trying to do, so it might be that on your system the times symbol was missing, which is why you got the box. Funny that the Unicode solution worked.

Related