Allan Sikk
2011-Oct-08 09:59 UTC
[R] Connecting points over missing observations in Lattice
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'). Thanks, Allan -- DrAllan Sikk Lecturer in Baltic Politics University College London, School of Slavonic and East European Studies 16 Taviton St, London WC1H 0BW, United Kingdom tel: +44 (0)20 7679 4872 http://www.homepages.ucl.ac.uk/~tjmsasi/ [[alternative HTML version deleted]]
David Winsemius
2011-Oct-08 18:11 UTC
[R] Connecting points over missing observations in Lattice
On Oct 8, 2011, at 4:59 AM, Allan Sikk 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').Can't you just use: xyplot(y1 + y2 ~ t, data=size, subset = !is.na(y2) )> Thanks, > AllanDavid Winsemius, MD Heritage Laboratories West Hartford, CT
Gabor Grothendieck
2011-Oct-09 23:45 UTC
[R] Connecting points over missing observations in Lattice
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