K. Maya
K. Maya

Reputation: 299

annotate equation in ggplot

curve(dnorm, from = -3, to = 3, n = 1000)
text(-2, 0.3, expression(f(x) == paste(frac(1, sqrt(2 * pi * sigma^2)),
                                       " ", e^{
                                         frac(-(x - mu)^2, 2 * sigma^2)
                                       })), cex = 1.2)

How can I add the above expression text in ggplot2? If I use expression then I get error saying input must be a vector, not an expression.

ggplot(data.frame(x = c(-3, 3)), aes(x))+
  stat_function(fun = dnorm)+
  annotate("text", label = , 
           x = -2, 
           y = 0.3, 
           color = "black") 

Upvotes: 2

Views: 654

Answers (1)

Duck
Duck

Reputation: 39613

Try this:

library(ggplot2)
#Label
Lab <- expression(f(x) == paste(frac(1, sqrt(2 * pi * sigma^2)),
                         " ", e^{
                           frac(-(x - mu)^2, 2 * sigma^2)
                         }))
#Code
ggplot(data.frame(x = c(-3, 3)), aes(x))+
  stat_function(fun = dnorm)+
  annotate("text", label = Lab, 
           x = -2, 
           y = 0.3, 
           color = "black",parse=T) 

Output:

enter image description here

Upvotes: 2

Related Questions