jean10
jean10

Reputation: 171

Displaying equation on graph (ggplot2)

I'm new to R and using the following codes to plot a graph:

p <- ggplot(data = data, aes(x, y, color=group)) +
     geom_smooth(method = "lm", se=FALSE, formula = lineq) + 
     stat_poly_eq(formula = lineq,
               eq.with.lhs = "italic(hat(y))~`=`~",
               aes(label = paste(..eq.label..)), 
               parse = TRUE) +         
     geom_point()

when I plot this, the equation currently displays as y^ = intercept + ax

How can I change the code so that the equation displays with the actual name of the variable?

For example, height^ = intercept + age * x

Thank you

Upvotes: 1

Views: 492

Answers (1)

MYaseen208
MYaseen208

Reputation: 23898

Try something like

library(ggpmisc)

p <- ggplot(data = data, aes(x = age, y = height, color=group)) +
     geom_smooth(method = "lm", se=FALSE, formula = lineq) + 
     stat_poly_eq(formula  = lineq,
               eq.with.lhs = "italic(height)~`=`~",
               eq.x.rhs    = "~italic(age)",
               aes(label   = paste(..eq.label..)), 
               parse = TRUE) +         
     geom_point()

Upvotes: 1

Related Questions