This maybe a silly question. I'm trying to figure out a way to draw a line from a data set which contain NA. Say, I have a set of data as: x <- c(1.1 2.2 NA 4.4 5.5) ; y <- c(1:5) # as x,y of point a, b, c, d, and e. I would like to plot this to a line by using dot-line to connect the two adjacent points before and after the NA, something like: (a)______(b)......(d)______(e) Any help will be very appreciated. -- Smiles, Timothy Huang ??? 43, Lane 525, Section 3, AnKang Road, XinDian City Taipei County, Taiwan 23156, Rep. of China ???23156????????525?43? Tel 886-2-2214-0517 Fax 886-2-2214-4201 Get Skype <http://www.skype.com/go/download> and call me for free. Web: www.dinodragon.com <http://www.dinodragon.com/> Blog: http://tw.myblog.yahoo.com/timd_huang/ Blog: http://bloguide.ettoday.com/dinodragon/ Web: http://www.meteoritefossilgallery.com/
On 17/04/2008, at 12:56 PM, DinoDragon wrote:> This maybe a silly question. I'm trying to figure out a way to draw > a line from a data set which contain NA. Say, I have a set of data as: > > x <- c(1.1 2.2 NA 4.4 5.5) ; y <- c(1:5) # as x,y of point a, b, > c, d, and e. > > I would like to plot this to a line by using dot-line to connect > the two > adjacent points before and after the NA, something like: (a)______ > (b)......(d)______(e)How's this? foo <- function(x,y,...) { plot(x,y,type="n",...) na <- apply(cbind(x,y),1,function(x){any(is.na(x))}) f <- c(na[-1],FALSE) x <- x[!na] y <- y[!na] f <- f[!na] n <- length(x) f <- f[-n] segments(x[-n],y[-n],x[-1],y[-1],lty=ifelse(f,3,1)) } set.seed(42) x <- sort(runif(20)) y <- rnorm(20) ix <- sample(1:20,3) iy <- sample(1:20,3) x[ix] <- NA y[iy] <- NA foo(x,y) cheers, Rolf Turner ###################################################################### Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
Try this which interpolates the NAs using na.approx from zoo drawing the interpolated series with lty=3 and then overwrites solid lines with the gaps using lines. library(zoo) plot(y ~ x, as.data.frame(na.approx(zoo(cbind(x, y)))), lty = 3, type = "l") lines(y ~ x) On Wed, Apr 16, 2008 at 8:56 PM, DinoDragon <timd_huang at yahoo.com> wrote:> This maybe a silly question. I'm trying to figure out a way to draw a line from a data set which contain NA. Say, I have a set of data as: > > x <- c(1.1 2.2 NA 4.4 5.5) ; y <- c(1:5) # as x,y of point a, b, c, d, and e. > > I would like to plot this to a line by using dot-line to connect the two > adjacent points before and after the NA, something like: (a)______(b)......(d)______(e) > > Any help will be very appreciated. > > -- > > > Smiles, > > Timothy Huang ??? > > 43, Lane 525, Section 3, AnKang Road, XinDian City > > Taipei County, Taiwan 23156, Rep. of China > > ???23156????????525?43? > > Tel 886-2-2214-0517 Fax 886-2-2214-4201 > > > Get Skype <http://www.skype.com/go/download> and call me for free. > > Web: www.dinodragon.com <http://www.dinodragon.com/> > > Blog: http://tw.myblog.yahoo.com/timd_huang/ > > Blog: http://bloguide.ettoday.com/dinodragon/ > Web: http://www.meteoritefossilgallery.com/ > > ______________________________________________ > 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. >