0

Given a data frame like :

df1= data.frame(x = c(1:50))
df1$val=df1$x*(-0.35)

I used the ggplot2 and added a regression line with the command

t=ggplot(df1, aes(x=val, y=x))+geom_smooth(method=lm) + geom_point()

In order to add the equation and the r value I tried the code from this question Adding Regression Line Equation and R2 on graph

but I am getting the error

Error in terms.formula(formula, data = data) : 
  'data' argument is of the wrong type 

Any ideas on how to fix this?

EDIT

The code I used

my_sts <- function(df1){
  m <- lm(df1$x ~ df1$val, df1);
  eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2, 
                   list(a = format(coef(m)[1], digits = 2), 
                        b = format(coef(m)[2], digits = 2), 
                        r2 = format(summary(m)$r.squared, digits = 3)))
  as.character(as.expression(eq));                 
}

tgen = t + geom_text(x = -10, y = 50, label = eq(df1), parse = TRUE)
Community
  • 1
  • 1
geo_dd
  • 283
  • 1
  • 5
  • 22
  • 1
    You need to include the code that is producing the error above. – nrussell Aug 20 '16 at 19:10
  • What code did you actually try? Please include a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) in your question. Don't refer to code else where since we don't know exactly how you used it. Show us exactly the parameters you passed. – MrFlick Aug 20 '16 at 19:10
  • What is lm_eqn here? – Pj_ Aug 20 '16 at 19:23
  • Oh that was a typo sorry. Thanks @Pj_ – geo_dd Aug 20 '16 at 19:26
  • 1
    Perhaps you meant `label = my_sts(df1)`? – dww Aug 20 '16 at 19:35
  • @dww I am embarrassed but you are right! that was my problem. Thank you! – geo_dd Aug 20 '16 at 19:39
  • 1
    Never do anything like this: `lm(df1$x ~ df1$val, df1)`. using the `df1$` extraction screws up the subsequent connection between df1 and the variables. (I can understand that doesn't seem right , but trust me.) – IRTFM Aug 20 '16 at 19:39

1 Answers1

1

This is copied from a console session. I corrected two things that I thought were errors: 1) as mention in my comment you should not use df1$ in a formula when you have a data argument, and 2) I think you mean to use my_sts(df1)

> df1= data.frame(x = c(1:50))
> df1$val=df1$x*(-0.35)
> my_sts <- function(df1){
+   m <- lm(x ~ val, df1);
+   eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2, 
+                    list(a = format(coef(m)[1], digits = 2), 
+                         b = format(coef(m)[2], digits = 2), 
+                         r2 = format(summary(m)$r.squared, digits = 3)))
+   as.character(as.expression(eq));                 
+ }
> t=ggplot(df1, aes(x=val, y=x))+geom_smooth(method=lm) + geom_point()
> tgen = t + geom_text(x = -10, y = 50, label = eq(df1), parse = TRUE)
Error in layer(data = data, mapping = mapping, stat = stat, geom = GeomText,  : 
  could not find function "eq"
> tgen = t + geom_text(x = -10, y = 50, label = my_sts(df1), parse = TRUE)
Warning message:
In summary.lm(m) : essentially perfect fit: summary may be unreliable
> print(tgen)

Seems to print well: Note that x an y roles are reversed, hence the coefficient being the inverse of the modeled factor.

enter image description here

IRTFM
  • 258,963
  • 21
  • 364
  • 487