Reputation:
I wonder if anyone can point me the way to arrange several graphs created with lattice in a grid.
The example:
library(lattice)
attach(mtcars)
# create factors with value labels
gear.f<-factor(gear,levels=c(3,4,5),
labels=c("3gears","4gears","5gears"))
cyl.f <-factor(cyl,levels=c(4,6,8),
labels=c("4cyl","6cyl","8cyl"))
# kernel density plot
a<-densityplot(~mpg,
main="Density Plot",
xlab="Miles per Gallon")
Ok, so I have created the a plot.
After reading previous post, I have learn how to arrange this in a grid with grid.arranje
library(latticeExtra)
library(gridExtra)
grid.arrange(a,a, nrow=2,ncol=2)
I want to generate a graph with two plots in the first row, two plots in the second and three plots in the third and forth row.
I have tried the following, witch doesn´t work
grid.arrange(a,a,a,a,ncol=2,(arrangeGrob(a,a,a,a,a,a,ncol=2)))
Is it possible to do want I want, starting from my code?
Upvotes: 4
Views: 173
Reputation: 77116
With gridExtra,
library(gridExtra)
a <- rectGrob(gp=gpar(fill="grey90"))
row12 <- arrangeGrob(a, a, a, a, ncol=2)
row34 <- arrangeGrob(a, a, a, a, a, a, ncol=3)
grid.arrange(row12, row34, ncol=1)
Upvotes: 3