Combining plots created by R base, lattice, and gg

2019-04-29 13:27发布

问题:

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?

回答1:

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() by ggplot() since use of qplot() is now discouraged.

library(ggplot2)
library(cowplot) # devtools::install_github("wilkelab/cowplot/")
library(lattice)

#1 base R (note formula format for base graphics)
p1 <- ~boxplot(mpg~cyl,
               xlab = "Number of Cylinders",
               ylab = "Miles per Gallon",
               data = mtcars)

#2 lattice
p2 <- bwplot(~mpg | cyl,
             xlab = "Number of Cylinders",
             ylab = "Miles per Gallon",
             data = mtcars)

#3 ggplot2
p3 <- ggplot(data = mtcars, aes(factor(cyl), mpg)) +
        geom_boxplot() +
        xlab("Number of Cylinders") +
        ylab("Miles per Gallon")

# cowplot plot_grid function takes all of these
# might require some fiddling with margins to get things look right
plot_grid(p1, p2, p3, rel_heights = c(1, .6), labels = c("a", "b", "c"))

The cowplot functions also integrate with the patchwork library for more sophisticated plot arrangements (or you can nest plot_grid() calls):

library(patchwork) # devtools::install_github("thomasp85/patchwork")
plot_grid(p1, p3) / ggdraw(p2)



回答2:

See the approach using gridBase described in the answer to this question: R: How should I create Grid-graphics?

library(grid)
library(gridBase)
library(lattice)
library(ggplot2)

grid.newpage()
pushViewport(viewport(layout = grid.layout(1, 3)))

# base graphics
vp <- pushViewport(viewport(layout.pos.row = 1, layout.pos.col = 1))
par(omi = gridOMI())
boxplot(mpg ~ cyl,
        xlab = "Number of Cylinders",
        ylab = "Miles per Gallon", data = mtcars)
popViewport()

# lattice plot
vp <- pushViewport(viewport(layout.pos.row = 1, layout.pos.col = 2))
par(fig = c(0.9, 1, 0.6, 0.9))
p <- bwplot(~ mpg | cyl,
            xlab = "Number of Cylinders",
            ylab = "Miles per Gallon",
            data = mtcars)
print(p, vp = vp, newpage = FALSE)
popViewport()

# ggplot
vp <- pushViewport(viewport(layout.pos.row = 1, layout.pos.col = 3))
mtcars$cyl <- as.factor(mtcars$cyl)
p <- qplot(cyl,
           mpg,
           data = mtcars,
           geom = ("boxplot"),
           fill = cyl,
           xlab = "Number of Cylinders",
           ylab = "Miles per Gallon")
print(p, vp = vp, newpage = FALSE)
popViewport()