Skip to content

lattice, latticeExtra: Adding moving averages to double y plot

1 message · Duncan Mackay

#
Hi Anna

I got down to 

plot2_3_concentration <-
xyplot(Value ~ Year, datamystuff23, groups = Type, ...

and got an error:
Error in eval(substitute(groups), data, environment(x)) : 
  object 'datamystuff23' not found

so I did not pursue further

Note : from ?doubleYScale
Note that most settings, like main/sub/legend/etc are taken only from obj1;
only the panel, axis and ylab are taken from obj2.

As we are now working with 2 plots you can reduce the complexity of your
plots and you may get away without having to use a panel function.
Try without a panel function and then a panel function. You may then get
away with a panel.xyplot in the panel without having to resort to
panel.groups.

Settings for the key are those found in the trellis.par.get()$superpose. ...
series ie. trellis.par.get()$superpose.line,
trellis.par.get()$superpose.symbol etc
You can setup par.settings adding 
superpose.symbol = list(...),
superpose.line = list( ...)
 
or what I have done

        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"),
        col = c(1:3) # for points
         )

and modify accordingly. I usually find it is easier to have a custom key as
I have to make changes to various parts of the key and it is easier to add
on things there.

Duncan

-----Original Message-----
From: Anna Zakrisson Braeunlich [mailto:anna.zakrisson at su.se] 
Sent: Sunday, 3 August 2014 15:10
To: Duncan Mackay
Subject: RE: [R] lattice, latticeExtra: Adding moving averages to double y
plot

Hi Duncan,

I think you have misunderstood my plot. I am able to change lty the way you
have plotted it with points on one axis and moving averages on the other. if
you run the code below, you find the plot that I need to do. data points and
moving averages for factor 1 on one yaxis (temperature) and factor 2 and 3
(data points and moving averages on the second y-axis: summer and winter
concentrations of compound X). See the plot below to better see what I mean.

1. My problem is to change lty for the axis where I have temperature. I
somehow need to specify that lty is depending on factor, which I am not able
to do. I have marked the position in plot2_3 where I have a problem. 

2. The second issue is the symbols (pch). I have managed to change these in
the plot, but cannot make them appear in the legend. How would I go about
this? See comments in script. 

Below is the full script with a correct plot with the issues at hand
labelled.

many thank's for your time!
Anna

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

# stuff1 is temperature
# stuff 2 and stuff 3 are concentration measurements of compound X

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)

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

table(mydata$Type)

# split stuff1 from stuff2/3 as they will read off different y-axes
stuff1mavg_points <- mydata[(mydata$Type) %in% c("stuff1"), ] #temperature
stuff23mavg_points <- mydata[(mydata$Type) %in% c("stuff2", "stuff3"), ] #
concentration

# plot stuff2/3 with data points and moving averages reading off y-axis 1:

plot2_3_concentration <- xyplot(Value ~ Year, datamystuff23, groups = Type,
                  allow.multiple = T,
                  distribute.type = TRUE,
                  par.settings = list(layout.heights = list(key.top = 1.5)),
                  col = "black", pch=c(2,15), 
                  ylab="concentration",
                  subscripts = TRUE,
                  panel = panel.superpose,
                  panel.groups = function(x, y, subscripts,
...,group.number) {
                    panel.xyplot(x, y, ...)
                    panel.xyplot(x, datamystuff23[subscripts,"mavg"], type =
"l", col="black",
                                 lty = c(1:2))  # HERE IS THE PROBLEM!
LTY!!!
                  })
plot2_3_concentration 

# If I  use l lty = c(1:2)[group.number], I get only one line!


# plot stuff 1: with data points and moving averages reading off y-axis 2
plot1_temperature<- xyplot(Value ~ Year, datamystuff1, groups = Type,
               allow.multiple = T,
               distribute.type = TRUE,
               par.settings = list(layout.heights = list(key.top = 1.5)),
               col = "black", pch=4,
               subscripts = TRUE,
               ylab="temperature",
               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", lty=3) #
HERE I AM ABLE TO MODIFY LTY
               })
plot1_temperature


doubleYScale(plot2_3_concentration, plot1_temperature, style1 = 0, style2=0,
add.ylab2 = TRUE,
             text = c("winter concentration cpd X", "summer concentration
cpd X",
                      "temperature"), lty=c(1:3), columns = 2, 
             col=c("black", "black", "black")) # HERE TWO LINES ARE FULLY
DRAWN!

update(trellis.last.object(),
       par.settings = simpleTheme(col = c("black", "black"), lty=c(1:3), 
                                  pch=c(2,4,15))) # WHY CAN I NOT SEE PCH IN
THE LEGEND?

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
.? `?. .><((((?>