Reputation: 67
I have a problem. I want to arrange 7 plots in a single page: 3 plots in a top line, 2 plots below and 2 other below. I could'nt make it work with layout(matrix=)) (I think because the top line has an odd number of plots). I have used aplot and image.plot to make the plots. Thanks. Henri
Upvotes: 0
Views: 845
Reputation: 160447
layout
can absolutely do an odd number of plots on the top. You just need to think of more than 1 image per matrix element. Since you want 3 across the top and 2 across the other rows, then the lowest common multiple is 6. Make a layout with 6 columns.
m <- matrix(c(1,1,2,2,3,3,
4,4,4,5,5,5,
6,6,6,7,7,7), ncol = 6, byrow = TRUE)
layout(m)
layout.show(7)
FYI, this works equally well for rows as columns. The only strict "requirements" using layout
are that all elements must be non-NA
, non-negative, contiguous numbers (no numbers can be skipped). "0" means to skip a cell (a "hole" in the plot). Negatives are ignored, NA
s are errors. It does not return an error if a set of plot numbers is not rectangular, but I won't guarantee that it'll always look like you want.
Upvotes: 2
Reputation: 2945
If you are using ggplot
, you might also consider using the patchwork
package for this to make it easy. For example:
Step 1: Create all your figures and assign them to objects (e.g., fig1
, fig2
etc.). For example:
fig1 <- ggplot(data = myData, mapping = aes(x = x, y = y) +
geom_point()
#...etc.
Step 2: To create the layout to specified:
library(patchwork)
(fig1 + fig2 + fig3) / (fig4 + fig5) / (fig6 + fig7)
Upvotes: 1