build.args.GOF.legend <- function(actual,
                                  forecast,
                                  weight
                                  ){


  RMSE <- sqrt(sum(weight * (actual - forecast)^2)/sum(weight))
  RMSE.scaled <- sqrt(sum(weight * (actual - forecast)^2) * sum(weight))/sum(actual * weight)

  mat <- format(cbind(RMSE, RMSE.scaled))

  rownames(mat) <- "full sample"
  colnames(mat) <- c("RMSE","RMSE/MeanActual")

  txt <- capture.output(print.noquote(mat))

  args <- list(text = list(label      = txt,
                           fontfamily = "mono"
                           ),
               border = TRUE,
               title = "goodness of fit statistics",
               cex.title = 1
               )

  return(args)
}

Data <- data.frame(yyyymm = rep(201001:201012, times=2L),
                   actual = runif(12*2),
                   forecast = runif(12*2),
                   weight = c(cumsum(runif(12)), cumsum(runif(12))),
                   cond   = gl(2, 12)
                   )

GOFlist <- by(data = subset(Data, select = -c(cond, yyyymm)),
              INDICES = Data$cond,
              FUN = function(x) do.call(build.args.GOF.legend, x),
              simplify = FALSE
              )

library(lattice)
library(grid)

plot <- xyplot(actual + forecast ~ yyyymm | cond,
               data = Data,
               layout = c(1,2),
               type = "l",
               panel = function(x,y,GOFlist,...){
                 panel.xyplot(x,y,...)
                 pn <- panel.number()
                 key <- draw.key(key = GOFlist[[pn]], draw = FALSE)
                 vp <- viewport(x = 1,
                                y = 1,
                                width = grobWidth(key),
                                height = grobHeight(key),
                                just = c("right","top")
                                )
                 pushViewport(vp)
                 grid.draw(key)
                 popViewport()
               },
               GOFlist = GOFlist
               )

print(plot)
