當(dāng)圖形的數(shù)據(jù)不是來(lái)源于同一數(shù)據(jù)集,在grid系統(tǒng)中實(shí)現(xiàn)一頁(yè)多圖或嵌入子圖最常見的方法是通過(guò)視圖窗口viewport和矩形網(wǎng)格grid。此外,還有幾種奇技淫巧:
-
方法1
grid繪圖系統(tǒng)(lattice和ggplot2)圖形的合并。
#ggplot2 graphics
library("ggplot2")
p1 <- ggplot(mtcars, aes(factor(cyl), disp)) +
geom_boxplot(aes(group = cyl))
#ggplot2 graphics
library("ggplot2")
p1 <- ggplot(mtcars, aes(factor(cyl), disp)) +
geom_boxplot(aes(group = cyl))
#lattice graphics
library("lattice")
p2 <- bwplot(disp ~ factor(cyl), data = mtcars , notch = F)
#ggplot2 graphics
p3 <- ggplot(mtcars, aes(mpg, disp)) +
geom_point()
library("grid)
pushViewport(viewport(x = 0.25, y = 0.75, width = 0.5, height = 0.5, just = "centre"))
print(p1, newpage = F)
popViewport()
pushViewport(viewport(x = 0.75, y = 0.75, width = 0.5, height = 0.5, just = "centre"))
print(p2, newpage = F)
popViewport()
pushViewport(viewport(x = 0.25, y = 0.25, width = 0.5, height = 0.5, just = "centre"))
print(p3, newpage = F)
popViewport()

-
方法2
grid繪圖系統(tǒng)(lattice和ggplot2)圖形的合并。
#ggplot2 graphics
library(ggplot2)
p1 <- ggplot(mtcars, aes(factor(cyl), disp)) +
geom_boxplot(aes(group = cyl))
#lattice graphics
library(lattice)
p2 <- bwplot(disp ~ factor(cyl), data = mtcars , notch = F)
library(gridExtra)
grid.arrange(p1,p2,nrow = 1)

-
方法3
此方法只適合ggplot2繪圖系統(tǒng)的圖形的合并。
#devtools::install_github("easyGgplot2", "kassambara")
library(easyGgplot2)
library(ggplot2)
#ggplot2 graphics
p1 <- ggplot(mtcars, aes(factor(cyl), disp)) +
geom_boxplot(aes(group = cyl))
#ggplot2 graphics
p3 <- ggplot(mtcars, aes(mpg, disp)) +
geom_point()
ggplot2.multiplot(p1, p3, cols = 2)
ggplot2.multiplot <- function (..., plotlist = NULL, cols = 2) {
plots <- c(list(...), plotlist)
numPlots = length(plots)
plotCols = cols
plotRows = ceiling(numPlots/plotCols)
grid::grid.newpage()
grid::pushViewport(grid::viewport(layout = grid::grid.layout(plotRows,
plotCols)))
vplayout <- function(x, y) grid::viewport(layout.pos.row = x,
layout.pos.col = y)
for (i in 1:numPlots) {
curRow = ceiling(i/plotCols)
curCol = (i - 1)%%plotCols + 1
print(plots[[i]], vp = vplayout(curRow, curCol))
}
}

-
方法4
此方法只適合ggplot2繪圖系統(tǒng)的圖形的合并。
library(ggplot2)
#ggplot2 graphics
p1 <- ggplot(mtcars, aes(factor(cyl), disp)) +
geom_boxplot(aes(group = cyl))
#ggplot2 graphics
p3 <- ggplot(mtcars, aes(mpg, disp)) +
geom_point()
library(Rmisc)
multiplot(p1, p3, cols = 2)

-
方法5
此方法只適合ggplot2繪圖系統(tǒng)的圖形的合并。
#ggplot2 graphics
library(ggplot2)
p1 <- ggplot(mtcars, aes(factor(cyl), disp)) +
geom_boxplot(aes(group = cyl))
#ggplot2 graphics
p3 <- ggplot(mtcars, aes(mpg, disp)) +
geom_point()
library(cowplot)
plot_grid(p1, NULL, NULL, p3, labels = c("A", "", "", "C"), ncol = 2)

-
方法6
在實(shí)際繪圖中,不少朋友困惑于如何將傳統(tǒng)繪圖和ggplot2繪圖系統(tǒng)在一頁(yè)中實(shí)現(xiàn)。本文將介紹一種新方法,參考Paul Murrell編寫的R Graphics Third Edition中第12章Combining Graphics Systems中的方法,在一頁(yè)中多圖展示3種箱線圖(ggplot2,lattice和傳統(tǒng)繪圖)。實(shí)現(xiàn)代碼:
library(gridGraphics)
#traditional graphics
cpfun <- function() {
boxplot(disp ~ cyl, data = mtcars)
}
pushViewport(viewport(x = 0.25, y = 0.75, width = 0.5, height = 0.5, just = "centre"))
grid.echo(cpfun, newpage = F, prefix = "cp")
upViewport()
#ggplot2 graphics
pushViewport(viewport(x = 0.75, y = 0.75, width = 0.5, height = 0.5, just = "centre"))
library(ggplot2)
p1 <- ggplot(mtcars, aes(factor(cyl), disp)) +
geom_boxplot(aes(group = cyl))
print(p1, newpage = F)
upViewport()
#lattice graphics
pushViewport(viewport(x = 0.25, y = 0.25, width = 0.5, height = 0.5, just = "centre"))
library(lattice)
p2 <- bwplot(disp ~ factor(cyl), data = mtcars , notch = F)
print(p2, newpage = F)
upViewport()
