Hi lattice users,

I would like to add 5-year moving averages to my double y-plot. I have three factors needs to be plotted with moving averages in the same plot. One of these reads off y-axis 1 and two from y-axis 2. I have tried to use the rollmean function from the zoo-packages, but I fail in insering this into lattice (I am not an experienced lattice user). I want to keep the data points in the plot.
Find below dummy data and the script as well as annotations further describing my question.

thank you in advance!
Anna Zakrisson

mydata<- data.frame(
  Year = 1980:2009,
  Type = factor(rep(c("stuff1", "stuff2", "stuff3"), each = 10*3)),
  Value = rnorm(90, mean = seq(90),
                sd = rep(c(6, 7, 3), each = 10)))

library(Lattice)
library(LatticeExtra)

stuff1data <- mydata[(mydata$Type) %in% c("stuff1"), ]
stuff12_3data <- mydata[(mydata$Type) %in% c("stuff2", "stuff3"), ]


# make moving averages function using zoo and rollmean:
library(zoo)
library(plyr)

f <- function(d)
{
  require(zoo)
  data.frame(Year = d$Year[5:length(d$Year)],
             mavg = rollmean(d$Value, 5))
}

# Apply the function to each group as well as both data frames:
madfStuff1 <- ddply(stuff1data, "Type", f)
madfStuff2_3 <- ddply(stuff12_3data, "Type", f)

# Some styles:
myStripStyle <- function(which.panel, factor.levels, ...) {
  panel.rect(0, 0, 1, 1,
             col = bgColors[which.panel],
             border = 1)
  panel.text(x = 0.5, y = 0.5,
             font=2,
             lab = factor.levels[which.panel],
             col = txtColors[which.panel])
}


myplot1 <- xyplot(Value ~ Year, data = stuff1data, col="black",
                   lty=1, pch=1,
                   ylab = "sweets", strip.left = F,
                   strip=myStripStyle,
                   xlab = ("Year"),
                  panel = function(x,y,...,subscripts){
                    panel.xyplot(x, y, pch = 1,col = "black")
                    panel.lmline(x,y,col = "black", data=madfStuff1) # here I presume that panel.lmline is wrong.
                    # I would like to have my 5 year moving average here, not a straight line.
                  })
myplot1


myplot2 <- xyplot(Value ~ Year, data = stuff12_3data, col="black",
                  lty=1, pch=1,
                  ylab = "hours", strip.left = F,
                  strip=myStripStyle,
                  xlab = ("Year"),
                  panel = function(x,y,...,subscripts){
                    panel.xyplot(x, y, pch = c(2:3),col = "black") ## what is this "pch" defining? Types?
                    #I would like to have different symbols and line types for stuff2 and stuff3
                    panel.lmline(x,y,col = "black", data=madfStuff2_3) # wrong! Need my moving averages here!
                  })
myplot2

doubleYScale(myplot1, myplot2, style1 = 0, style2=0, add.ylab2 = TRUE,
             text = c("stuff1", "stuff2", "stuff3"), columns = 2, col="black")

# problem here is that I end up with two lines. I need a double y-plot with one moving average plots that are read off y-axis 1
# and two that reads off y-axis 2. I need to keep the data points in the plot.

update(trellis.last.object(),
       par.settings = simpleTheme(col = c("black", "black"), lty=c(1:3), pch=c(1:3))) # how come that I only get
# lines in my legend text and not the symbols too. I thought "pch" would add symbols?!?


Anna Zakrisson Braeunlich
PhD student

Department of Ecology, Environment and Plant Sciences
Stockholm University
Svante Arrheniusv. 21A
SE-106 91 Stockholm
Sweden/Sverige

Lives in Berlin.
For paper mail:
Katzbachstr. 21
D-10965, Berlin
Germany/Deutschland

E-mail: anna.zakrisson@su.se
Tel work: +49-(0)3091541281
Mobile: +49-(0)15777374888
LinkedIn: http://se.linkedin.com/pub/anna-zakrisson-braeunlich/33/5a2/51b

><((((º>`•. . • `•. .• `•. . ><((((º>`•. . • `•. .• `•. .><((((º>`•. . • `•. .• `•. .><((((º>

	[[alternative HTML version deleted]]

