[R] Regression line does not show on scatterplot

(Ted Harding) Ted.Harding at wlandres.net
Tue Dec 18 22:15:51 CET 2012


Sorry, I made a mistake in re-writing your code below. See at [***]
On 18-Dec-2012 21:00:28 Ted Harding wrote:
 On 18-Dec-2012 20:09:36 Beatriz González Domínguez wrote:
> Hello,
> 
> I have done a scatterplot and now would like to add its regression
> line but it does not show.
> Below, the code I have used. 
> 
> lm3 <- lm(data$S_pH_KCl2.5_BCx~data$B_OleicoPF_BCx_per)
> plot(data$S_pH_KCl2.5_BCx, data$B_OleicoPF_BCx_per)
> abline(lm3)
> 
> I have been able to do the complete operation using the software
> STATISTICA but it would be great to do it with R.
> 
> If you require more details please get in touch.
> 
> Thanks a lot!
> Bea
 
By the look of things you have either the regression or the plot
the wrong way round. I suspect it is the regression. So try:

[***] The following should be correct (I previously mis-copied
[***] your variable names).
Either:
##lm3 <- lm(data$S_pH_KCl2.5_BCx~data$B_OleicoPF_BCx_per)
  lm3 <- lm(data$B_OleicoPF_BCx_per ~ data$S_pH_KCl2.5_BCx)
  plot(data$S_pH_KCl2.5_BCx, data$B_OleicoPF_BCx_per)
  abline(lm3)

Or:
  lm3 <- lm(data$S_pH_KCl2.5_BCx ~ data$B_OleicoPF_BCx_per)
##plot(data$S_pH_KCl2.5_BCx, data$B_OleicoPF_BCx_per)
  plot(data$B_OleicoPF_BCx_per, data$S_pH_KCl2.5_BCx)
  abline(lm3)

The point is that in lm(V~U) the variable "U" is taken as
corresponding the the x-axis (independent variable), and the
variable "V" to the y-axis (dependent variable).

Similarly for plot(U,V).

So, for lm3 <- lm(V~U), abline(lm3) will plot the fitted V-values
(y-axis) against the U-values (x-axis).

Your original code was equivalent to:

  lm3 <- lm(V~U)
  plot(V,U)
  abline(lm3)

whereas it should be
Either:
  lm3 <- lm(V~U)
  plot(U,V)
  abline(lm3)
Or:
  lm3 <- lm(U~V)
  plot(V,U)
  abline(lm3)

Hoping this helps,
Ted.

-------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at wlandres.net>
Date: 18-Dec-2012  Time: 21:15:47
This message was sent by XFMail




More information about the R-help mailing list