[R] Multiple Plotting help (lines don't always connect)
hadley wickham
h.wickham at gmail.com
Tue Aug 19 01:50:23 CEST 2008
>> Doesn't a line plot inherently display a set of linear interpolations?
>
> Yes. And your point is?
>
> Compare:
>
> x <- 1:10
> y <- rep(1:2,5)
> y[5] <- NA
> y0 <- approx(x,y,xout=1:10)
> plot(y0,type="l")
>
> with
>
> foo(x,y)
>
> where
>
> 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))
> }
>
> There is a difference in what you are telling the reader/viewer.
But why is 4-6 special? What makes it different to 1-2 and 2-3 and
... Without more information there is no way for the reader to tell
where the measured values are - i.e. they could be every 1 with 1
missing values, every 0.5 with 2 missing values, or other completely
different pattern.
I'd argue what you (almost) always want is:
plot(y0,type="l")
points(y)
then you can see exactly where the measurements are. The only time
that this isn't necessary is when you have a perfectly regular
sampling on x, and the reader knows that it exists (i.e. from a
caption or prior knowledge)
Hadley
--
http://had.co.nz/
More information about the R-help
mailing list