Dear HelpeRs, Given: x <- rnorm(50) y <- rnorm(50) plot(x,y) abline(lm(x ~ y)) Is there a way to plot just a portion of the line? Say for values of x> 2.0 or x > -2.0 and x < 4.0. (Still fitting all the points.)Thank you, jab -- John Bollinger, CFA, CMT www.BollingerBands.com If you advance far enough, you arrive at the beginning.
you could try something like the following: x <- rnorm(50) y <- rnorm(50) obj <- lm(y ~ x) par(mfrow = c(2, 2)) plot(x, y, main = "x < -1") x. <- c(min(x), -1) y. <- predict(obj, data.frame(x = x.)) lines(x., y.) plot(x, y, main = "x > 1") x. <- c(1, max(x)) y. <- predict(obj, data.frame(x = x.)) lines(x., y.) plot(x, y, main = "x > -1 & x < 1") x. <- c(-1, 1) y. <- predict(obj, data.frame(x = x.)) lines(x., y.) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "BBands" <bbands at gmail.com> To: "R-Help" <r-help at stat.math.ethz.ch> Sent: Tuesday, January 16, 2007 5:16 PM Subject: [R] plot portion of a line> Dear HelpeRs, > > Given: > x <- rnorm(50) > y <- rnorm(50) > plot(x,y) > abline(lm(x ~ y)) > > Is there a way to plot just a portion of the line? Say for values of > x >> 2.0 or x > -2.0 and x < 4.0. (Still fitting all the points.) > > Thank you, > > jab > -- > John Bollinger, CFA, CMT > www.BollingerBands.com > > If you advance far enough, you arrive at the beginning. > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Use segments. In the following we overlay the black abline with a wider red segments line segment: set.seed(1) x <- rnorm(50) y <- rnorm(50) plot(y ~ x) y.lm <- lm(y ~ x) abline(y.lm) # omit this line if black abline not wanted x0 <- c(-2, 4) y0 <- predict(y.lm, list(x = x0)) segments(x0[1], y0[1], x0[2], y0[2], col = "red", lwd = 2) On 1/16/07, BBands <bbands at gmail.com> wrote:> Dear HelpeRs, > > Given: > x <- rnorm(50) > y <- rnorm(50) > plot(x,y) > abline(lm(x ~ y)) > > Is there a way to plot just a portion of the line? Say for values of x > > 2.0 or x > -2.0 and x < 4.0. (Still fitting all the points.) > > Thank you, > > jab > -- > John Bollinger, CFA, CMT > www.BollingerBands.com > > If you advance far enough, you arrive at the beginning. > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
Try the clipplot function from the TeachingDemos package:> x <- rnorm(50) > y <- rnorm(50) > plot(x,y) > clipplot( abline(lm(y~x), col='red'), xlim=c(1,3)) > clipplot( abline(lm(y~x), col='blue'), xlim=c(-2,1))Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at intermountainmail.org (801) 408-8111> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of BBands > Sent: Tuesday, January 16, 2007 9:16 AM > To: R-Help > Subject: [R] plot portion of a line > > Dear HelpeRs, > > Given: > x <- rnorm(50) > y <- rnorm(50) > plot(x,y) > abline(lm(x ~ y)) > > Is there a way to plot just a portion of the line? Say for values of x > > 2.0 or x > -2.0 and x < 4.0. (Still fitting all the points.) > > Thank you, > > jab > -- > John Bollinger, CFA, CMT > www.BollingerBands.com > > If you advance far enough, you arrive at the beginning. > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >