Dear R users, I am a new R user. My problem is very simple: I want to add a linear regression line to the plot(type="p"), codes are as below: x<-c(10,20,40,80) y<-c(30,40,100,200) plot(x,y,type="p") lines(lm(x~y),col="red",lwd=1.5) I got only plot without linear line. Many thanks ahead for your advice! Xipei Wang [[alternative HTML version deleted]]
Dear R users, I am a new R user. My problem is very simple: I want to add a linear regression line to the plot(type="p"), codes are as below: x<-c(10,20,40,80) y<-c(30,40,100,200) plot(x,y,type="p") lines(lm(x~y),col="red",lwd=1.5) I got only plot without linear line. Many thanks ahead for your advice! Xipei Wang E-mail:wangxipei@gmail.com [[alternative HTML version deleted]]
Such things are very easy with the ggplot2 package
install.packages("ggplot2")
library(ggplot2)
Dataset <- data.frame(A = c(10,20,40,80), B = c(30,40,100,200))
ggplot(data = Dataset, aes(x = A, y = B)) + geom_point() +
geom_smooth(method = "lm")
More info and example on the ggplot2 website: http://had.co.nz/ggplot2/
Best regards,
Thierry
------------------------------------------------------------------------
----
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek
team Biometrie & Kwaliteitszorg
Gaverstraat 4
9500 Geraardsbergen
Belgium
Research Institute for Nature and Forest
team Biometrics & Quality Assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium
tel. + 32 54/436 185
Thierry.Onkelinx at inbo.be
www.inbo.be
To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to
say what the experiment died of.
~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data.
~ Roger Brinner
The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of
data.
~ John Tukey
> -----Oorspronkelijk bericht-----
> Van: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] Namens wangxipei
> Verzonden: donderdag 13 januari 2011 9:35
> Aan: r-help
> Onderwerp: [R] add a linear regression line to the plot
>
>
> Dear R users,
> I am a new R user. My problem is very simple: I want to
> add a linear regression line to the plot(type="p"), codes are
> as below:
>
> x<-c(10,20,40,80)
> y<-c(30,40,100,200)
> plot(x,y,type="p")
> lines(lm(x~y),col="red",lwd=1.5)
>
> I got only plot without linear line.
> Many thanks ahead for your advice!
>
> Xipei Wang
> E-mail:wangxipei at gmail.com
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
On Thu, 13-Jan-2011 at 04:35PM +0800, wangxipei wrote:
|>
|> Dear R users,
|> I am a new R user. My problem is very simple: I want to add a linear
regression line to the plot(type="p"), codes are as below:
|>
|> x<-c(10,20,40,80)
|> y<-c(30,40,100,200)
|> plot(x,y,type="p")
|> lines(lm(x~y),col="red",lwd=1.5)
|>
|> I got only plot without linear line.
|> Many thanks ahead for your advice!
What about if you tried
lines(lm(y~x),col="red",lwd=1.5)
My guess is that would be more likely to work.
--
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.
___ Patrick Connolly
{~._.~} Great minds discuss ideas
_( Y )_ Average minds discuss events
(:_~*~_:) Small minds discuss people
(_)-(_) ..... Eleanor Roosevelt
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.
?abline On Thu, Jan 13, 2011 at 12:29 AM, wo <bellewong@163.com> wrote:> Dear R users, > I am a new R user. My problem is very simple: I want to add a linear > regression line to the plot(type="p"), codes are as below: > > x<-c(10,20,40,80) > y<-c(30,40,100,200) > > plot(x,y,type="p") > lines(lm(x~y),col="red",lwd=1.5) > > I got only plot without linear line. > Many thanks ahead for your advice! > > Xipei Wang > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Hi,> lines(lm(x~y),col="red",lwd=1.5)should be abline(lm(y~x),col="red",lwd=1.5) hth. Am 13.01.2011 09:35, schrieb wangxipei:> > Dear R users, > I am a new R user. My problem is very simple: I want to add a linear regression line to the plot(type="p"), codes are as below: > > x<-c(10,20,40,80) > y<-c(30,40,100,200) > plot(x,y,type="p") > lines(lm(x~y),col="red",lwd=1.5) > > I got only plot without linear line. > Many thanks ahead for your advice! > > Xipei Wang > E-mail:wangxipei at gmail.com > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Eik Vettorazzi Department of Medical Biometry and Epidemiology University Medical Center Hamburg-Eppendorf Martinistr. 52 20246 Hamburg T ++49/40/7410-58243 F ++49/40/7410-57790
Hi, if it should be "lines" so you can do that xy.lm <- lm(y~x) lines(x, xy.lm$coeff[1] + x*xy.lm$coeff[2], col="blue", lwd=3) Regards ep -- View this message in context: http://r.789695.n4.nabble.com/add-a-linear-regression-line-to-the-plot-tp3215455p3216099.html Sent from the R help mailing list archive at Nabble.com.