Hello

Within my panel function, I am using draw.key to create a custom key for each panel. I'm trying to use grobWidth to calculate the correct width for the viewport, but it appears to be disregarding the fontfamily when computing the width. Attached is an example; any ideas what I'm doing wrong?

Many thanks
Ben
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)
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to