在ggplot2的注释中解析两个符号(== 4 == 2 * 2)

时间:2015-10-14 18:25:38

标签: r parsing plot ggplot2 annotations

是否有一种简单的方法可以在ggplot2的注释中解析同一文本中的两个等号或者我是否需要注释两次? (即在下图中用:替换=?)

我可以用这样的注释进行绘图

require(ggplot2)
f <- data.frame(x = c(0, 1/1.1), y = c(1/.9, 0))
c <- ggplot(f, aes(x, y))
c + stat_smooth(method = "lm", se=F)  
+ annotate("text", x=.6,y=.75, label=(paste0("slope:","frac(1, f[1])==", 
coef(lm(f$y ~ f$x))[2])), parse=TRUE)

这给了我这个, ====

但我不能有两个这样的等号,

f <- data.frame(x = c(0, 1/1.1), y = c(1/.9, 0))
c <- ggplot(f, aes(x, y))
c + stat_smooth(method = "lm", se=F)  + annotate("text", x=.6,y=.75,
label=(paste0("slope==","frac(1, f[1])==", coef(lm(f$y ~ f$x))[2])), parse=TRUE)

我收到此错误。

Error in parse(text = lab) : <text>:1:23: unexpected '=='
1: slope==frac(1, f[1])==
                          ^

在我的实际案例中,我有几个分数,我意识到我的工作示例很简单,有人可能会问为什么用:替换=,但这是一个有效的例子。

1 个答案:

答案 0 :(得分:6)

f <- data.frame(x = c(0, 1/1.1), y = c(1/.9, 0))
c <- ggplot(f, aes(x, y))
c <- c + stat_smooth(method = "lm", se=F) + annotate("text", x=.6,y=.75, label=(paste0("slope==~frac(1,f[1])==", coef(lm(f$y ~ f$x))[2])), parse=TRUE)

plot(c)

编辑。情节是:

enter image description here