[R] Connecting lines across missing data points, xyplot

Deepayan Sarkar deepayan.sarkar at gmail.com
Thu Jun 26 23:29:12 CEST 2008


On 6/26/08, David Afshartous <dafshartous at med.miami.edu> wrote:
>
>
>  All,
>
>  I have data across 5 time points that I am graphing via xyplot, along with
>  error bars.  For one of the variables I have missing data for two of the
>  time points.  The code below is okay but I can't seem to get the lines to
>  connect across the missing time points.  Does anyone now how to rectify
>  this?

Well, I don't think joining lines ignoring NA-s would be a good idea
in general. If you want that behaviour, you need to omit the NA's,
either using

  junk <- na.omit(junk)

before the xyplot call, or add

  subset = !is.na(Aldo),

to the xyplot call.

-Deepayan

>
>  Cheers,
>  David Afshartous
>
>
>
>  library(lattice)
>  ## the data
>  junk = data.frame(
>  Visit = as.factor(rep(seq(1,5), 2)),
>  Drug = rep(c("D", "P"), each = 5),
>  Aldo = c(13, NA, NA, 15, 14, 12, NA, NA, 14, 13),
>  SE.Aldo = c(3,  NA, NA, 3, 3, 2, NA, NA, 2, 2),
>  lower.ci.Aldo = c(10, NA, NA, 12, 11,  10,NA,  NA, 12, 11),
>  upper.ci.Aldo = c(16, NA, NA, 18, 17, 14, NA, NA, 16, 15)
>  )
>
>  ## functions for the error bars
>  prepanel.ci <- function(x, y, ly, uy, subscripts, ...) {
>     x <- as.numeric(x)
>     ly <- as.numeric(ly[subscripts])
>     uy <- as.numeric(uy[subscripts])
>     list(ylim = range(y, uy, ly, finite = TRUE)) }
>  panel.ci <- function(x, y, ly, uy, subscripts, pch = 16, ...) {
>     x <- as.numeric(x)
>     y <- as.numeric(y)
>     ly <- as.numeric(ly[subscripts])
>     uy <- as.numeric(uy[subscripts])
>     panel.arrows(x, ly, x, uy, col = "black",
>                  length = 0.25, unit = "native",
>                  angle = 90, code = 3)
>     panel.xyplot(x, y, pch = 16, ...)}
>
>
>  ## the plot, but visit 1 not connected to visit 4
>  xyplot(Aldo ~ as.numeric(Visit), xlab="Visit", ylab="Aldo",
>         groups=Drug,
>         data=junk,
>         ly = junk$lower.ci.Aldo,
>         uy = junk$upper.ci.Aldo,
>         prepanel = prepanel.ci,
>         panel = panel.superpose,
>         panel.groups = panel.ci,
>         type="b",
>         auto.key = list(space = "top",  text = c( "D","P"), points = FALSE,
>  lines = TRUE, columns=2),
>  par.settings = list(superpose.line = list(lty = c(1,5), col=c('black',
>  'black') ) )        )
>
>  ______________________________________________
>  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.
>



More information about the R-help mailing list