Skip to content
Prev 342694 / 398500 Next

lattice, latticeExtra: Adding moving averages to double y plot

Hi Anna

I am still unsure what you want

1 do you want 1 panel or 3?
2 do you want 1 y axis on the left and 1 on the right 
or 1 y axis on the left and 2 on the right
                                         or 1 on the right varying with
groups in a multipanel plot

For starters try

xyplot(Value ~ Year, mydata, groups = Type,
        allow.multiple = T,
        distribute.type = TRUE,
        par.settings = list(layout.heights = list(key.top = 1.5)),  #
separate key from bottom a bit more
        col = c("black","black","black"), # for points
        key = list(text = list(label = c("stuff1","stuff2","stuff3")),
                   lines = list(col = 1:3),
                   cex   = 0.8,
                   title = "stuff type",
                   cex.title = 0.9,
                   space = "bottom"),
        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(1:3)[group.number], type = "l")
 })
 
OR

  xyplot(Value ~ Year|Type, mydata,
        allow.multiple = T,
        aspect = 0.75,
        layout = c(1,3),
        groups = Type,
        distribute.type = TRUE,
        par.settings = list(layout.heights = list(key.top = 1.5)),  #
separate key from bottom a bit more
        scales = list(alternating = F),
        col = c("black","black","black"),
        subscripts = TRUE,
        key = list(text = list(label = c("stuff1","stuff2","stuff3")),
                   lines = list(col = 1:3),
                   cex   = 0.8,
                   title = "stuff type",
                   cex.title = 0.9,
                   space = "bottom"),
        panel = panel.superpose,
        panel.groups = function(x, y, subscripts, ...,group.number) {
          panel.xyplot(x, y, ...)
          panel.xyplot(x, mydata[subscripts,"mavg"], col =
c(1:3)[group.number], type = "l")
 })

have a look at 

 ?lattice::axis.default
for yscale.components

This means that you only need 1 xyplot object

if you use doubleYScale you need to xyplot objects 
1 for the points and another for the averages

These are then superimposed with the doubleYScale

Also read and re read ?xyplot
names(trellis.par.get()) give you a list of the names in trellis.par.get() 
and trellis.par.get() gives you all the settings used by  argument
par.settings in xyplot and those in the lattice series
par.settings may help you with themes.

http://lmdvr.r-forge.r-project.org/figures/figures.html
may be instructive

Duncan

-----Original Message-----
From: Anna Zakrisson Braeunlich [mailto:anna.zakrisson at su.se] 
Sent: Wednesday, 30 July 2014 23:59
To: Duncan Mackay
Subject: RE: [R] lattice, latticeExtra: Adding moving averages to double y
plot

Hi Duncan and many thank's for your help!
I could see your first message perfectly.

You solved my problem to some extent. What my problem is, is that I need a
double y plot with two of these moving averages belonging to one axis (both
are temperature measurements, say Stuff 2 and 3, measured in degrees C) the
other one reading off the second axis (a nutrient measurement in mM (say
Stuff1)).

How can I plot Stuff2 and stuff3 moving averages in one plot and stuff1 in
another and then combine them using doubleYscale?

I also have some other annotated questions regarding linetypes. I could
change symbols using pch, but not the line type. Why? See code below:

Many many thank's once again. 

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"), ]
stuff2_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(stuff2_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])
}    

mydata$mavg <-
  c(rep(NA,4), madfStuff1[,3], # what is rep(NA,4) doing?
    rep(NA,4), subset(madfStuff2_3, Type== "stuff2",3, drop = T), # is "3"
referring to the number of factors?
    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("black","black","black"),
       subscripts = TRUE,
       par.settings = simpleTheme(lty=c(1:3), pch=c(1:3)), #why can I change
symbol (pch), but not line type (lty)?
       panel = panel.superpose,
       panel.groups = function(x, y, subscripts, ...,group.number) {
         panel.xyplot(x, y, ...)
         panel.xyplot(x, mydata[subscripts,"mavg"], col =
                        c("black","black","black")[group.number], type =
"l") # tried to change lty here too lty=c(1:3), but # without success
       })

# how can I add a legend specifying the linetype AS WELL AS symbol?
# I tried using:   text = c("stuff1", "stuff2", "stuff3"), columns = 2  but
could not make it work.




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 at su.se
Tel work: +49-(0)3091541281
Mobile: +49-(0)15777374888
LinkedIn: http://se.linkedin.com/pub/anna-zakrisson-braeunlich/33/5a2/51b
.? `?. .><((((?>