Hi, I have recently been playing with the grid package in an attempt to create some pages containing multiple lattice plots on the same page. However, when I specify a grid layout with different widths, such as: pushViewport(viewport(layout = grid.layout(1, 2, unit(c(2, 1), "null")))) the individual graphs do not end up as the same height - which is a feature I would prefer to have. A complete example is as follows: ### Start of example library(lattice) library(Hmisc) library(grid) # Incidence data testData <- data.frame( strata = rep(c("CHF : Yes", "CHF : No"), each = 20), ae = rep(paste("Adverse Event", 1:10), each = 2), trt = rep(c("Active", "Placebo"), 20), pct = runif(40, 1, 30) ) # RR data testData2 <- data.frame( strata = rep(c("CHF : Yes", "CHF : No"), each = 10), ae = paste("Adverse Event", 1:10), rr = runif(20, 0.5, 5) ) testData2$lower = testData2$rr / 2 testData2$upper = testData2$rr * 2 # Combined plot testPlots <- function(relativeWidth) { plot1<- dotplot( ae ~ pct | strata, groups = trt, data = testData, layout = c(1, 2), xlab = "Percent", auto.key = list(space = "top", columns = 2) ) plot2 <- Dotplot( ae ~ Cbind(rr, log10(lower), log10(upper)) | strata, data = testData2, panel = function(...) { panel.Dotplot(...) panel.abline(v = 0, col = 'red', lty = 2) }, layout = c(1, 2), scales = list( x = list(log = T, at = c(0.125, 0.25, 0.5, 1, 2, 4, 8, 16, 32)), y = list(draw = F) ), xlab = "Relative Risk with 95% CI", ylab = "", key = list(text = list("")) ) grid.newpage() pushViewport(viewport(layout = grid.layout(2, 1, heights = unit(c(1, 6), "null")))) pushViewport(viewport(layout.pos.col = 1, layout.pos.row = 1)) grid.text("Analysis of Relative Risks of various Adverse Events") upViewport() pushViewport(viewport(layout.pos.col = 1, layout.pos.row = 2)) ### Change the relative width of the 2 presented graphics pushViewport(viewport(layout = grid.layout(1, 2, unit(c(relativeWidth, 1), "null")))) pushViewport(viewport(layout.pos.col = 1, layout.pos.row = 1)) print(plot1, newpage = F) upViewport() pushViewport(viewport(layout.pos.col = 2, layout.pos.row = 1)) print(plot2, newpage = F) upViewport() } # Everything is fine, both graphs maintain the same y-axis testPlots(1) # The second graph is now "taller" than the first one win.graph() testPlots(3) ##### End of example I've been through the documentation of both lattice and grid, and I have not been able to find the answer. I would appreciate any solution! Regards, James Price. -- View this message in context: http://www.nabble.com/Changing-graphics-height-when-using-grid-and-lattice-tf3996724.html#a11350733 Sent from the R help mailing list archive at Nabble.com.
Deepayan Sarkar
2007-Jun-28 22:24 UTC
[R] Changing graphics height when using grid and lattice
On 6/28/07, Jim Price <price_ja at hotmail.com> wrote:> > Hi, > > I have recently been playing with the grid package in an attempt to create > some pages containing multiple lattice plots on the same page. However, when > I specify a grid layout with different widths, such as: > > pushViewport(viewport(layout = grid.layout(1, 2, unit(c(2, 1), "null")))) > > the individual graphs do not end up as the same height - which is a feature > I would prefer to have.The default "padding" between components of a graph is defined as a proportion of the plot area. In particular, it's unit(0.01, "snpc"), which makes it the same on both axes. The problem is that when you have two grid viewports, unit(0.01, "snpc") may give different physical length. One option is to set all paddings to 0, which can be done with myLatticeSettings <- function() list(layout.heights list(top.padding = 0, main.key.padding = 0, key.axis.padding = 0, axis.xlab.padding = 0, xlab.key.padding = 0, key.sub.padding = 0, bottom.padding = 0), layout.widths list(left.padding = 0, key.ylab.padding = 0, ylab.axis.padding = 0, axis.key.padding = 0, right.padding = 0) ) trellis.par.set(myLatticeSettings()) This may not be what you want, so another option is to set the paddings to something absolute; e.g. myLatticeOptions <- function() list(layout.heights list(top.padding = list(x = 1, units = "mm", data = NULL), main.key.padding = list(x = 1, units = "mm", data = NULL), key.axis.padding = list(x = 1, units = "mm", data = NULL), axis.xlab.padding = list(x = 1, units = "mm", data = NULL), xlab.key.padding = list(x = 1, units = "mm", data = NULL), key.sub.padding = list(x = 1, units = "mm", data = NULL), bottom.padding = list(x = 1, units = "mm", data = NULL)), layout.widths list(left.padding = list(x = 1, units = "mm", data = NULL), key.ylab.padding = list(x = 1, units = "mm", data = NULL), ylab.axis.padding = list(x = 1, units = "mm", data = NULL), axis.key.padding = list(x = 1, units = "mm", data = NULL), right.padding = list(x = 1, units = "mm", data = NULL)) ) lattice.options(myLatticeOptions()) I'm not particularly attached to the "snpc" solution, so I may change them at some point. -Deepayan