[R] Scaling Lattice Graphics for tikzDevice

Elliot Joel Bernstein elliot.bernstein at fdopartners.com
Fri Feb 18 18:34:59 CET 2011


I'm trying to use lattice graphics to produce some small plots for inclusion in a LaTeX file. I want the LaTeX fonts to be used in the plots, but to be scaled down to match the size of the plot. I have written the following code to apply a scaling factor to all the "cex" and "padding" entries in the trellis parameters, but there is still a large white space between the key and the top of the plot area. Does anyone know how I can get rid of that (or if I'm going about this all wrong and there's a much cleaner way)?

## ------ ##
## test.R ##
## ------ ##

require(tikzDevice)                                                                                                                                                                
require(lattice)                                                                                                                                                                   
                                                                                                                                                                                   
rescale.pars <- function(cex.factor     = 0.5,                                                                                                                                     
                         padding.factor = 0.25,                                                                                                                                    
                         pars           = trellis.par.get()) {                                                                                                                     
                                                                                                                                                                                   
  result <- NULL                                                                                                                                                                   
  for (i in seq_along(pars)) {                                                                                                                                                     
                                                                                                                                                                                   
    if (names(pars)[[i]] == "cex") {                                                                                                                                               
                                                                                                                                                                                   
      if (is.null(result)) {                                                                                                                                                       
        result <- list()                                                                                                                                                           
      }                                                                                                                                                                            
      result <- c(result, cex=cex.factor*pars[[i]])                                                                                                                                
                                                                                                                                                                                   
    } else if (grepl("padding$", names(pars)[[i]])) {                                                                                                                              
                                                                                                                                                                                   
      if (is.null(result)) {                                                                                                                                                       
        result <- list()                                                                                                                                                           
      }                                                                                                                                                                            
      eval(parse(text=sprintf("result <- c(result, %s=padding.factor*pars[[i]])", names(pars)[[i]])))                                                                              
                                                                                                                                                                                   
    } else if (inherits(pars[[i]], "list")) {                                                                                                                                      
                                                                                                                                                                                   
      temp <- rescale.pars(cex.factor, padding.factor, pars[[i]])                                                                                                                  
      if (!is.null(temp)) {                                                                                                                                                        
        result[[names(pars)[[i]]]] <- temp                                                                                                                                         
      }                                                                                                                                                                            
                                                                                                                                                                                   
    }                                                                                                                                                                              
  }                                                                                                                                                                                
                                                                                                                                                                                   
  return (result)                                                                                                                                                                  
}                                                                                                                                                                                  
                                                                                                                                                                                   
x <- 1:100                                                                                                                                                                         
y <- cumsum(rnorm(length(x), 1, 10))                                                                                                                                               
z <- cumsum(rnorm(length(x), 1, 20))                                                                                                                                               
                                                                                                                                                                                   
tikz("test-plot.tex", width=2, height=2)                                                                                                                                           
                                                                                                                                                                                   
xyplot(y + z ~ x,                                                                                                                                                                  
       main = "My Plot",                                                                                                                                                           
       xlab = "x",                                                                                                                                                                                     
       auto.key=list(text=c("Line 1", "Line 2"), points=FALSE, lines=TRUE),                                                                                                        
       grid = TRUE,                                                                                                                                                                
       par.settings = rescale.pars())                                                                                                                                              
                                                                                                                                                                                   
dev.off()                                                     

## ---------- ##
## END R CODE ##
## ---------- ##

Thanks.

- Elliot

-----------------------------
Elliot Joel Bernstein, Ph.D. | Research Associate | FDO Partners, LLC
134 Mount Auburn Street | Cambridge, MA | 02138
Phone: (617) 503-4619 | Email: elliot.bernstein at fdopartners.com



More information about the R-help mailing list