geotheory
geotheory

Reputation: 23660

Plot window division with viewports in R

I'm trying to achieve a simple division of the plot window with viewports, but something's not right. The code below wants to achieve the first plot in the left hand 2/3rds of the window and the second in the right hand 1/3rd. Grateful for an idea what I'm missing:

pushViewport(viewport())
pushViewport(viewport(x=0, y=0, width=.66, height=1, just=c("left","top")))
plot(rnorm(100))
popViewport(1)
pushViewport(viewport(x=.66, y=0, width=.33, height=1, just=c("left","top")))
plot(1:20, 1:20, add=T)
popViewport(2)

Alternatively I wonder if it's possible to replicate the following ggplot method with base plotting. The code below is illustrative of method but does not work as is incomplete - but is demo'd at rstudio-pubs-static. But is the method transferable?

a <- qplot(1:10, rnorm(10), main = "a")
b <- qplot(1:10, rnorm(10), main = "b")
c <- qplot(1:10, rnorm(10), main = "c")
grid.newpage()
pushViewport(viewport(layout = grid.layout(2, 2)))
vplayout <- function(x, y) viewport(layout.pos.row = x, layout.pos.col = y)
print(a, vp = vplayout(1, 1:2))  # key is to define vplayout
print(b, vp = vplayout(2, 1))
print(c, vp = vplayout(2, 2))

enter image description here

Upvotes: 0

Views: 1302

Answers (1)

csgillespie
csgillespie

Reputation: 60462

If you want to use base graphics, then layout is an easy option:

layout(matrix(c(1, 2), 1, 2, byrow=TRUE), 
              widths=c(2/3, 1/3))
layout.show(n=2)
plot(rnorm(100))
plot(runif(100))

Upvotes: 4

Related Questions