[R] lattice, latticeExtra: Adding moving averages to double y plot

Duncan Mackay dulcalma at bigpond.com
Tue Jul 29 01:45:05 CEST 2014

I do not know what happened to my last email as this are set up as plain
text so I am sending the code again so I hope this works

I am not sure what you wanted exactly but this will plot the points and
lines of the average.
I have not worried about the 2nd axis

Here is one way of doing things  by combining the averages into the
It makes it easier that way as you do not have to match up the x values

# combine averages into mydata
 mydata$mavg <-
 c(rep(NA,4), madfStuff1[,3],
   rep(NA,4), subset(madfStuff2_3, Type== "stuff2",3, drop = T),
   rep(NA,4), subset(madfStuff2_3, Type== "stuff3",3, drop = T))

 xyplot(Value ~ Year, mydata, groups = Type,
        allow.multiple = T,
        distribute.type = TRUE,
        col = c("red","blue","cyan"),
         subscripts = TRUE,
        panel = panel.superpose,
        panel.groups = function(x, y, subscripts, ...,group.number) {
                  panel.xyplot(x, y, ...)
                   panel.xyplot(x, mydata[subscripts,"mavg"], col =
c("red","blue","cyan")[group.number], type = "l")


BTW libraries are case sensitive as well. Is it you editor putting capitals?

Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2351
Email: home: mackay at northnet.com.au

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Anna Zakrisson Braeunlich
Sent: Monday, 28 July 2014 16:38
To: r-help at r-project.org
Subject: [R] lattice, latticeExtra: Adding moving averages to double y plot

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)))


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

# make moving averages function using zoo and rollmean:

f <- function(d)
  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,
             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,
                   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.

myplot2 <- xyplot(Value ~ Year, data = stuff12_3data, col="black",
                  lty=1, pch=1,
                  ylab = "hours", strip.left = F,
                  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!

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

# 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

       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

Anna Zakrisson Braeunlich
PhD student

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

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

E-mail: anna.zakrisson at 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]]

More information about the R-help mailing list