Dear all, I am using abline(lm ...) to insert a linear trendline through a portion of my data (e.g. dataset[,36:45]). However, I am finding that whilst the trendline is correctly displayed and representative of the data portion I've chosen, the line continues to run beyond this data segment and continues until it intersects the vertical axes at each side of the plot. How do I display the line so that it only runs between point 36 and 45 (as shown in the example above) as doesn't continue to display a line throughout the rest of the plot space? Many thanks, Steve _________________________________________________________________ View your other email accounts from your Hotmail inbox. Add them now. http://clk.atdmt.com/UKM/go/167688463/direct/01/
On Oct 9, 2009, at 5:50 AM, Steve Murray wrote:> > Dear all, > > I am using abline(lm ...) to insert a linear trendline through a > portion of my data (e.g. dataset[,36:45]). However, I am finding > that whilst the trendline is correctly displayed and representative > of the data portion I've chosen, the line continues to run beyond > this data segment and continues until it intersects the vertical > axes at each side of the plot. > > How do I display the line so that it only runs between point 36 and > 45 (as shown in the example above) as doesn't continue to display a > line throughout the rest of the plot space? >?segments> Many thanks, > > Steve >David Winsemius, MD Heritage Laboratories West Hartford, CT
Hi Steve,>> However, I am finding that ... the trendline ... continues to run beyond >> this data segment >> and continues until it intersects the vertical axes at each side of the >> plot.Your "best" option is probably Prof. Fox's reg.line function in package car. ## library(car) ?reg.line reg.line Regards, Mark. smurray444 wrote:> > > Dear all, > > I am using abline(lm ...) to insert a linear trendline through a portion > of my data (e.g. dataset[,36:45]). However, I am finding that whilst the > trendline is correctly displayed and representative of the data portion > I've chosen, the line continues to run beyond this data segment and > continues until it intersects the vertical axes at each side of the plot. > > How do I display the line so that it only runs between point 36 and 45 (as > shown in the example above) as doesn't continue to display a line > throughout the rest of the plot space? > > Many thanks, > > Steve > > _________________________________________________________________ > View your other email accounts from your Hotmail inbox. Add them now. > http://clk.atdmt.com/UKM/go/167688463/direct/01/ > ______________________________________________ > 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. > >-- View this message in context: http://www.nabble.com/Trendline-for-a-subset-of-data-tp25818425p25821972.html Sent from the R help mailing list archive at Nabble.com.
<CB247343-29EC-495A-BA8E-D5A466BABE53 at comcast.net> Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Thanks for flagging up the 'segments' command. However=2C I'm having trouble getting it to work - this is probably due to me misunderstanding the documentation for this command. The plot section of my script appears as follows: test3 <- 36:45 plot(data_means=2C type=3D"b"=2C pch=3D4=2C ylab=3D""=2C xlab=3D""=2C xaxt=3D"n"=2C yaxt=3D"n"=2C col=3D3=2C ylim=3Dc(250=2C380)) abline(lm(data_means[36:45] ~ test3)=2C lty=3D2) As you can see=2C I'm only plotting points 36 to 45 from the object 'data_means'. This intentionally results in much white space along the x-axis until point 36 is reached. However=2C when inserting the trendline=2C this runs along the entire length of the x-axis instead of just through points 36 to 45. Ideally=2C a line which slightly overshoots the data subset would look the best=2C but one constrained to the extents of the 36:45 would also do the job just fine. So=2C my question is=2C how do I use 'segments' (or otherwise) to create a linear trendline which only extends through points 36:45? (and overshoots at either end very slightly=2C if possible=2C rather than running along the entire length of the x-axis). Many thanks again=2C Steve ----------------------------------------> CC: r-help at r-project.org > From: dwinsemius at comcast.net > To: smurray444 at hotmail.com > Subject: Re: [R] Trendline for a subset of data > Date: Fri=2C 9 Oct 2009 09:27:43 -0400 > > > On Oct 9=2C 2009=2C at 5:50 AM=2C Steve Murray wrote: > >> >> Dear all=2C >> >> I am using abline(lm ...) to insert a linear trendline through a >> portion of my data (e.g. dataset[=2C36:45]). However=2C I am finding >> that whilst the trendline is correctly displayed and representative >> of the data portion I've chosen=2C the line continues to run beyond >> this data segment and continues until it intersects the vertical >> axes at each side of the plot. >> >> How do I display the line so that it only runs between point 36 and >> 45 (as shown in the example above) as doesn't continue to display a >> line throughout the rest of the plot space? >> > > ?segments > >> Many thanks=2C >> >> Steve >> > > David Winsemius=2C MD > Heritage Laboratories > West Hartford=2C CT >=0A_________________________________________________________________=0AView your other email accounts from your Hotmail inbox. Add them now.=0A=
Others have show how to use the segments function, but this can also be done using the original abline function along with the clip function. Here is an example: plot( iris$Petal.Width, iris$Petal.Length, col=c('red','green','blue')[iris$Species]) tmp <- levels(iris$Species) tmp2 <- par('usr') for(i in 1:3) { fit <- lm( Petal.Length ~ Petal.Width, data=iris, subset= Species==tmp[i] ) tmpx <- with(iris, Petal.Width[ Species==tmp[i] ] ) clip( min(tmpx), max(tmpx), tmp2[3], tmp2[4] ) abline( fit, col=c('red','green','blue')[i] ) } Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Steve Murray > Sent: Friday, October 09, 2009 3:51 AM > To: r-help at r-project.org > Subject: [R] Trendline for a subset of data > > > Dear all, > > I am using abline(lm ...) to insert a linear trendline through a > portion of my data (e.g. dataset[,36:45]). However, I am finding that > whilst the trendline is correctly displayed and representative of the > data portion I've chosen, the line continues to run beyond this data > segment and continues until it intersects the vertical axes at each > side of the plot. > > How do I display the line so that it only runs between point 36 and 45 > (as shown in the example above) as doesn't continue to display a line > throughout the rest of the plot space? > > Many thanks, > > Steve > > _________________________________________________________________ > View your other email accounts from your Hotmail inbox. Add them now. > http://clk.atdmt.com/UKM/go/167688463/direct/01/ > ______________________________________________ > 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.