1

我正在尝试plotmath在绘图标签中使用 R 数学符号。在标签中,我尝试使用等号,例如

ggplot(data=NULL) +
  geom_point(aes(x=0.5, y=0)) +
  geom_text(aes(x=0.5, y=-0.1),
            label = paste("x == frac(3,2) == 1.5"), parse=TRUE) +
  xlim(-1,1) +
  ylim(-1, 1)

但是,我收到以下错误:

Error in parse(text = text[[i]]) : <text>:1:16: Unexpected '=='
1: x == frac(3,2) ==
                   ^

是否可以在标签中使用两个等号?

(这是这个问题的后续问题。)

4

2 回答 2

3

您可以使用paste来连接这两个语句,但您可能需要通过在第二个表达式的左侧使用空白变量名来作弊。

ggplot(data=NULL) +
  geom_point(aes(x=0.5, y=0)) +
  geom_text(aes(x=0.5, y=-0.1),
            label = paste("paste(x == frac(3,2), ` ` == 1.5)"), parse=TRUE) +
  xlim(-1,1) +
  ylim(-1, 1)

在此处输入图像描述

于 2022-01-31T13:08:47.333 回答
3
ggplot(NULL) +
  geom_point(aes(x = 0.5, y = 0)) +
  annotate("text", x = 0.5, y = -0.2, label = expression(paste("x = ", frac(3, 2), " = 1.5"))) +
  annotate("text", x = 0.5, y = 0.2,  label = "~x == ~frac(3, 2) == ~1.5", parse = T) +
  annotate("text", x = 0.5, y = -0.4, label = expression({x == frac(3, 2)} == 1.5)) +
  xlim(-1, 1) +
  ylim(-1, 1)
于 2022-01-31T12:14:00.783 回答