[R] Scaling Lattice Graphics for tikzDevice

Deepayan Sarkar deepayan.sarkar at gmail.com
Sat Feb 19 05:02:24 CET 2011


On Fri, Feb 18, 2011 at 11:04 PM, Elliot Joel Bernstein
<elliot.bernstein at fdopartners.com> wrote:
> 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)

I have not (yet) used tikzDevice, so don't know what impact it will
have, but in my experience with pdf(), it's usually more effective to
create a larger file and then scale it when including it in the .tex
file.

-Deepayan

> 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
>
> ______________________________________________
> R-help at 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.
>



More information about the R-help mailing list