[R] Changing graphics height when using grid and lattice

Jim Price price_ja at hotmail.com
Thu Jun 28 22:47:00 CEST 2007


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.



More information about the R-help mailing list