Samantha Reynolds
2010-Apr-07 17:17 UTC
[R] Creating two lines of best fit on a scatter plot
Hello I am trying to plot two lines of best fit on a scatter plot. I'm plotting number of visits (y) against personality (x), half the plots are blue for patchchoice one and the other plots are red for patchchoice two (these options come from one factor, see below) . So i need a line for the patch ones and another line for the patch twos. How would i go about doing this? I know i may need to use the subset command, but I'm not entirely sure how to do this as I am relatively new to R example of data birdid timetaken numvisits ptachchoice time bold 1087 8 10 1 AM 0 1087 28 6 1 PM 0 1087 13 3 2 AM 0 1087 121 0 2 PM 0 1046 121 0 1 AM 1 1046 121 0 1 PM 1 Thanks Sam
Samantha Reynolds
2010-Apr-07 17:30 UTC
[R] Creating two lines of best fit on a scatter plot
Hello I am trying to plot two lines of best fit on a scatter plot. I'm plotting number of visits (y) against personality (x), half the plots are blue for patchchoice one and the other plots are red for patchchoice two (these options come from one factor, see below) . So i need a line for the patch ones and another line for the patch twos. How would i go about doing this? I know i may need to use the subset command, but I'm not entirely sure how to do this as I am relatively new to R example of data birdid timetaken numvisits ptachchoice time bold 1087 8 10 1 AM 0 1087 28 6 1 PM 0 1087 13 3 2 AM 0 1087 121 0 2 PM 0 1046 121 0 1 AM 1 1046 121 0 1 PM 1 Thanks Sam
lm1 <- lm(y~x, data=mydata, subset=mydata$patchchoice==1) lm2 <- lm(y~x, data=mydata, subset=mydata$patchchoice==2) abline(lm1, col="Blue") abline(lm2, col="Red" ) Should do it. Not likely the best way but a way. Andy. andydolman@gmail.com On 7 April 2010 19:17, Samantha Reynolds <sam.m.reynolds@hotmail.com> wrote:> Hello > > I am trying to plot two lines of best fit on a scatter plot. > > I'm plotting number of visits (y) against personality (x), half the plots > are blue for patchchoice one and the other plots are red for patchchoice > two (these options come from one factor, see below) . So i need a line for > the patch ones and another line for the patch twos. How would i go about > doing this? I know i may need to use the subset command, but I'm not > entirely sure how to do this as I am relatively new to R > > example of data > birdid timetaken numvisits ptachchoice time bold > 1087 8 10 1 AM 0 > 1087 28 6 1 PM 0 > 1087 13 3 2 AM 0 > 1087 121 0 2 PM 0 > 1046 121 0 1 AM 1 > 1046 121 0 1 PM 1 > > > Thanks > > Sam > > ______________________________________________ > 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]]