How to get Y-Intercept annotated in ggplot2? Current method giving odd results. --- NEWBIE ---

StackOverflow https://stackoverflow.com/questions/23072975

  •  03-07-2023
  •  | 
  •  

Question

So I tried two different methods for obtaining and annotating the equation of the line for my correlation scatter-plot using R and I'm getting results which, to me, seem odd and are different than those which seem less odd in SPSS.

I apparently don't have enough 'reputation points' to insert an image, but here is the code I am using...

model <- lm(hads ~ fatigue, mydata)
eqn <- as.character(as.expression(
substitute(italic(y) == a + b * italic(x) * "," ~~ italic(r)^2 ~ "=" ~ r2,
list(a = format(coef(model)[1], digits=3),
b = format(coef(model)[2], digits=3),
r2 = format(summary(model)$r.squared, digits=2)
))))
eqn

hwd <- ggplot(mydata, aes(x=hads, y=fatigue)) + geom_point(shape=21)
hwp + scale_x_continuous(name="HADS Depression Score") + scale_y_continuous(name="Daytime Fatigue") + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) + stat_smooth(method=lm) + annotate("text", label=eqn, parse=TRUE, x=Inf, y=-Inf, hjust=1.1, vjust=-.5)

So this gives me an equation which reads as: y=0.432+0.642*x, r^2=0.27

Which is odd, since my y intercept isn't at 0.432.

On SPSS I get: y=3.38+0.42*x, r^2=0.267

The actual scatterplots are basically identical on both SPSS and R, but I don't know how to get the formula to match that of SPSS, which is clearly appropriate, at least from what I know/can tell.

Thank you :) And sorry, I'm a huge newb

Was it helpful?

Solution

Without your data it's hard to verify but I suspect the only problem is that you are regressing had ~ fatigue in your model but you're plotting x = hads, y = fatigue. The formula in lm should be of the form y ~ model so I think you just have your variables mixed up in the model, try lm(fatigue ~ hads, mydata) and see if that gives you the same result as SPSS.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top