Question

I am using the grid package to do a multi-figure graph:

# load libraries
library(grid)
library(ggplot2)
library(gridSVG)

# create some data
p <- ggplot(mtcars, aes(wt, mpg))

# push Viewport and create layout
pushViewport(viewport(layout = grid.layout(nrow = 5, ncol = 4)))
matrixindex = cbind(rep(1:5,each =4), rep(1:4,times=5))

#fill viewport
for (k in 1:20){
print(p+geom_point(),
vp=viewport(layout.pos.row=matrixindex[k,1],layout.pos.col=matrixindex[k,2]))}

# export as SVG
gridToSVG("trial.svg","none","none")

Now, I would like to produce a second figure, with a changed layout (just one row, but again 4 columns). But the individual plots within the figure should have the same size like in the 5x4 layout. How can I achieve this?

Was it helpful?

Solution

You can use the heights and / or widths argument to grid.layout to fix the sizes, eg:

pushViewport(viewport(layout = grid.layout(heights = unit(0.2 , "npc" ) ,nrow = 1, ncol = 4)))

enter image description here

'npc' means normalised parent coordinates, so 0.2 takes 1/5 the viewport.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top