geo_dd - 1 year ago 132

R Question

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 ggplot2: 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)

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

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.

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**