Brad
Brad

Reputation: 127

How can I adjust the width of rows with layout in R

I have 8 plots that I would like to plot all one page. Each plot is made using base graphics, and I would prefer to stick to that rather than use lattice or ggplot.

The scheme below is of a page in which each digit denotes which plot # occupies that proportion of the page. Is there some way to do this with layout or any other base function?

1111122
3333444
5556666
7788888

Some code that I have so far:

pdf("test.pdf",height=30,width=10)
widths = c(5/7,2/7,4/7,3/7,3/7,4/7,2/7 5/7) # this doesn't work

x=layout(matrix(1:8,nrow=4,ncol=2,byrow=T), widths=widths,
         heights=rep(1/4,4))

for (ix in 1:4){        

    plot(rnorm(100))  
    plot(rnorm(100))
}
dev.off()

Upvotes: 3

Views: 854

Answers (1)

Sven Hohenstein
Sven Hohenstein

Reputation: 81683

You can specify a matrix for the layout and use the layout function.

mat <- t(sapply(1:4, function(x) 
                       rep.int(c((x - 1) * 2 + 1, (x - 1) * 2 + 2), c(6 - x, 1 + x))))
#      [,1] [,2] [,3] [,4] [,5] [,6] [,7]
# [1,]    1    1    1    1    1    2    2
# [2,]    3    3    3    3    4    4    4
# [3,]    5    5    5    6    6    6    6
# [4,]    7    7    8    8    8    8    8

layout(mat)

for (i in 1:8) {
  plot(rnorm(10))
}

If the same number is repeated in the layout matrix, the plot uses the space corresponding to this number.

enter image description here

Upvotes: 3

Related Questions