parse two signs (==4==2*2) in ggplot2's annotate

微笑、不失礼 提交于 2020-01-13 10:15:54

问题


Is there a simply way to parse two equal signs in the same text in ggplot2's annotate or do I have to annotate twice? (i.e. replace : by = in the plot below?)

I can plot with annotate like this

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)

Which gives me this,

but I cannot have two equal signs like this,

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)

I get this error.

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

In my real case I have several fractions, I realize that my working example is very simply and one might ask why replace : by =, but it's a working example.


回答1:


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)

Edited. Plot is:



来源:https://stackoverflow.com/questions/33132881/parse-two-signs-4-22-in-ggplot2s-annotate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!