I know how to combine plots created by R graphics. Just do something like
attach(mtcars)
par(mfrow = c(3,1))
hist(wt)
hist(mpg)
hist(disp)
However, now I have plots by three different graphic systems
# 1
attach(mtcars)
boxplot(mpg~cyl,
xlab = "Number of Cylinders",
ylab = "Miles per Gallon")
detach(mtcars)
# 2
library(lattice)
attach(mtcars)
bwplot(~mpg | cyl,
xlab = "Number of Cylinders",
ylab = "Miles per Gallon")
detach(mtcars)
# 3
library(ggplot2)
mtcars$cyl <- as.factor(mtcars$cyl)
qplot(cyl, mpg, data = mtcars, geom = ("boxplot"),
xlab = "Number of Cylinders",
ylab = "Miles per Gallon")
The par
method doesn't work anymore. How can I combine them?
See the approach using
gridBase
described in the answer to this question: R: How should I create Grid-graphics?I have been adding support for these kinds of problems to the cowplot package. (Disclaimer: I'm the maintainer.) The examples below require R 3.5.0 and the latest development version of cowplot. Note that I rewrote your plot codes so the data frame is always handed to the plot function. This is needed if we want to create self-contained plot objects that we can then format or arrange in a grid. I also replaced
qplot()
byggplot()
since use ofqplot()
is now discouraged.The cowplot functions also integrate with the patchwork library for more sophisticated plot arrangements (or you can nest
plot_grid()
calls):