Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 928
Eric Fail Avatar asked Oct 14 '15 18:10

Eric Fail


1 Answers

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:

enter image description here

like image 179
Alexey Ferapontov Avatar answered Nov 17 '22 04:11

Alexey Ferapontov