包含数学符号 $\\times$ 的图表标题。

4
我想在绘图标题中添加数学乘法符号(如小写x)。
我尝试使用latex2exp但无效,有人可以帮助我吗?
reprex代码如下:
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$"))

它会产生以下输出结果:

enter image description here

我希望正方形(表示无法识别的符号)显示为这个符号:enter image description here。看起来其他符号(例如 alpha)使用此方法可以工作,但我无法弄清楚为什么乘号会导致问题。

只要它适用于LaTeX字体(LM Roman 10),我就不必使用latex2exp作为解决方案。

非常感谢您提前的帮助。


1
你能在标题中使用unicode ×,例如:"Title containing times sign here: \u00D7"吗? - Ian Campbell
太好了,可以运行!非常感谢! :)(如果您将此作为答案发布,我可以接受它并关闭问题。) - Ivo
2个回答

6

对于我而言,使用 plotmath 工具可以实现:操作符%*%将显示为乘号。例如,

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: " %*% ""))

我认为这就是latex2exp尝试做的事情,因此在您的系统上可能缺少了乘号,这就是为什么会出现方框的原因。有趣的是,Unicode解决方案起作用了。

创建于2021-06-14,使用reprex包(v2.0.0)


5

一种方法是使用Unicode代码,代表乘法符号

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

任何字符串都可以使用\u和4位十六进制代码添加Unicode字符。您可以从操作系统或通过Google找到适当的代码。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接