[R] Connecting points over missing observations in Lattice

Gabor Grothendieck ggrothendieck at gmail.com
Mon Oct 10 01:45:56 CEST 2011


On Sat, Oct 8, 2011 at 5:59 AM, Allan Sikk <a.sikk at ucl.ac.uk> wrote:
> Hello,
>
> I'm trying to plot connected time series of two variables in a lattice plot:
> xyplot(y1 + y2 ~ t,  data=size, type="b")
>
> y2 has missing data for some of the observations and some points are
> therefore not connected. It would make theoretical sense to connect the
> points - is there a way of doing that? (Without filling the obserations
> using package 'zoo').
>

Break it up into lines and points and draw each separately (first
three approaches) or use a custom panel (approach 4):

# set up data
library(zoo)
library(lattice)
DF <- with(anscombe, data.frame(x = 1:11, y1, y2))
DF[2, 2] <- DF[3, 3] <- NA
z <- read.zoo(DF, FUN = identity)

# approach 1
xyplot(cbind(na.approx(z), z), type = list("l", "l", "p", "p"), screen
= 1, col = 1:2)

# approach 2
xyplot(na.approx(z), screen = 1, col = 1:2)
trellis.focus()
panel.points(z[, 1], col = 1)
panel.points(z[, 2], col = 2)
trellis.unfocus()

# approach 3
xyplot(z, screen = 1, type = "n")
trellis.focus()
panel.points(na.omit(z[, 1]), col = 1, type = "o")
panel.points(na.omit(z[, 2]), col = 2, type = "o")
trellis.unfocus()

# approach 4
xyplot(y1 + y2 ~ x, DF, panel = panel.superpose,
   panel.groups = function(x, y, subscripts, groups, ..., group.number)
     with(na.omit(data.frame(x, y)), panel.lines(x, y, type = "o", col
= group.number)))

-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list