Dear R users,

I am trying to use xyplot to draw group mean and CI. The following is the
sample code. But I want:

   1. Use different colors and symbols to draw individual points, CI and
   the lines connect group means from different time points;
   2. Add jitters to x axis to allow CIs not be overlapped

Could anyone modify the attached code to achieve this?

Thanks





library(lattice)
set.seed(123)
src <- data.frame(time = rep(c(1, 6, 24), rep(6,3)), trt =
rep(rep(c('C','T'), each=3), 3))
src <- transform(src, x=sqrt(time)+2*(trt=='C')+rnorm(18), trt =
ordered(trt, levels=c('C','T')))
src <- src[-5,]
src$m <- ave(src$x, src[c('time','trt')], FUN = mean)
src$sd <- ave(src$x, src[c('time','trt')], FUN = sd)
src$n <- ave(src$x, src[c('time','trt')], FUN = length)
src <- transform(src, se = sd/sqrt(n))
src <- transform(src, cl=m-se, cu=m+se)

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, my, uy, subscripts, ...)
  {
    x <- as.numeric(x);    y <- as.numeric(y);  my <-
as.numeric(my[subscripts])
    ly <- as.numeric(ly[subscripts]);  uy <- as.numeric(uy[subscripts])
    panel.xyplot(x, y, ...)
    panel.arrows(x, ly, x, uy, length = 0.25, unit = "native", angle = 90,
code = 3)
    panel.lines(x, my)
  }

par.settings <- list(superpose.symbol = list(col = c("red", "green",
"blue"),
                       fill = c("red", "green", "blue"), pch=15:17),
                      superpose.line = list(col = c("red", "green",
"blue")))
windows(width=10.67, height=6.60)
xyplot(x ~ time, xlab="Time", ylab="X", groups=trt, data=src, trt=src$trt,
       ly = src$cl, uy = src$cu, mx = src$time, my = src$m, type="p",
       prepanel = prepanel.ci, panel = panel.superpose, panel.groups =
panel.ci,
       auto.key = list(space = "top",  points = TRUE, lines = TRUE,
columns=2),
       par.settings = par.settings)

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to