0

I'm struggling to combine in a meaningful way d_ply and xyplot over a big database of sales, to plot two panels per week (one with a chart of the average price and one with cumulated quantities).

My action plan is to prepare the dataframe, subsetting it and use the subset with a combination of xyplot (to create the panels per week) and d_ply (to apply xyplot over all the different product codes on sale).

Let's assume I have the following type of dataframe (dummy code) and useful values to subset it:

 library(lattice)
library(latticeExtra)
library(plyr)
set.seed(1)
mydf <- as.data.frame(cbind(COD=rep(c("AB", "CD", "EF", "GH", "IJ"),20), SPEC = rep(c("a","b","c","d","e","f","g","h","i","j"),10)))
mydf$dates <- seq(as.Date("2014-09-01"), by=1, length.out=25)
mydf$x <- as.numeric(rnorm(100))
mydf$z <- as.numeric(rnorm(100))
mydf$deltaxz <- mydf$x-mydf$z
before=as.Date("2014-09-11")
after=as.Date("2014-09-03")

mydf=mydf[mydf$dates<=before & mydf$dates>=after,]

What I want is to get the following trellis objects for each COD in the dataframe:

graph1 <- xyplot (deltaxz ~ dates | SPEC, data=df, type=c("p","g"), col=c("black"), layout=c(1,3))
graph2 <- xyplot (1/deltaxz ~ dates | SPEC, data=df, type=c("p","g"), col=c("red"), layout=c(1,3))

What I thought was to apply the useful plyr functions, and my choice was d_ply so I wrote the following lines:

graph1 <- function(df) {assign("graph.1", xyplot (deltaxz ~ dates | SPEC, data=df, type=c("p","g"), col=c("black"), layout=c(1,3)), envir=.GlobalEnv)}
graph2 <- function(df) {assign("graph.2", xyplot (1/deltaxz ~ dates | SPEC, data=df, type=c("p","g"), col=c("red"), layout=c(1,3)), envir=.GlobalEnv)}

Now if I try to use the functions above combined with d_ply, I don't get a trellis object, but a null object:

graphic1 <- d_ply(mydf, .(mydf$COD), graph1, .print=T)
graphic2 <- d_ply(mydf, .(mydf$COD), graph2, .print=T)

The reason I need two trellis object is because I want to plot them one under the other to show something (that is evident using the actual sales figures but not the dummy dataframe). This will be a different challenge, so for now I stick to my request. Any suggestion?
Thanks in advance,
MZ

MaZe
  • 239
  • 1
  • 4
  • 13

1 Answers1

1

First of all, what's with assign() in the graph1/graph2 functions? That seems completely unnecessary. So just change those to

graph1 <- function(df) {xyplot (deltaxz ~ dates | SPEC, data=df, type=c("p","g"), col=c("black"), layout=c(1,3))}
graph2 <- function(df) {xyplot (1/deltaxz ~ dates | SPEC, data=df, type=c("p","g"), col=c("red"), layout=c(1,3))}

and secondly, the d_ply is specifically intended to return nothing (NULL). If you want to gather the results in a list, then use

graphic1 <- dlply(mydf, .(mydf$COD), graph1)
graphic2 <- dlply(mydf, .(mydf$COD), graph2)

Then you can get at the individual graphs with graphic1$AB and so on.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • Thanks MrFlick! Now I see it was pretty easy but I was so stubborn that I didn't want to change my previous coding (or better, I was unsure what to touch in it). I will now try to plot them together in a sensible way, probably using `l_ply` Thanks again! – MaZe Jan 30 '15 at 08:47