[R] plotting legend-grob with grid.layout

Johannes Radinger JRadinger at gmx.at
Thu May 10 14:12:55 CEST 2012


Hi,

I am using ggplot2 and arrange differnet plots into
one viewport by dividing it into rows and columns:


pushViewport(viewport(layout = grid.layout(nrow=2,ncol=2,widths = unit(c(50, 50), "mm"),heights = unit(c(50, 50), "mm"))))

with following function I can extract the legend of a previously defined
plot (ggplot2-plot) as a grob:

legend <- function(plot){
	tmp <- ggplot_gtable(ggplot_build(plot))
	leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
	legend <- tmp$grobs[[leg]]
}

The plot themselves are sent to the viewport with:

vplayout <- function(x, y)
	viewport(layout.pos.row = x, layout.pos.col = y)
print(p1, vp = vplayout(1, 1))


But, how can I put the legend into the viewport (e.g. colum=1,row=2)...

I tried with draw.grob() which works, but then I can define the posiiton...
This grid-arraning-viewport stuff is very confusing to me, so maybe someone can help out here...

/johannes

-- 

Jetzt informieren: http://mobile.1und1.de/?ac=OM.PW.PW003K20328T7073a



More information about the R-help mailing list