Beatriz González Domínguez
2012-Dec-18 20:09 UTC
[R] Regression line does not show on scatterplot
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 [[alternative HTML version deleted]]
You swapped the x and y variables in the plot command. lm(y~ x) but plot(x, y) On Tue, Dec 18, 2012 at 3:09 PM, Beatriz Gonz?lez Dom?nguez <harimaguadas at gmail.com> 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-- Sarah Goslee http://www.functionaldiversity.org
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! > BeaBy the look of things you have either the regression or the plot the wrong way round. I suspect it is the regression. So try: Either: ##lm3 <- lm(data$S_pH_KCl2.5_BCx~data$B_OleicoPF_BCx_per) lm3 <- lm(data$S_pH_KCl2.5_BCx_per~data$B_OleicoPF_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$S_pH_KCl2.5_BCx_per, data$B_OleicoPF_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:00:25 This message was sent by XFMail
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! > BeaBy 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