ggplot2趣图(六):如何将lattice,ggplot2和
2019-04-30 本文已影响11人
单核苷酸多态性
当图形的数据不是来源于同一数据集,在grid系统中实现一页多图或嵌入子图最常见的方法是通过视图窗口viewport和矩形网格grid。此外,还有几种奇技淫巧:
-
方法1
grid绘图系统(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绘图系统(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绘图系统的图形的合并。
#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绘图系统的图形的合并。
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绘图系统的图形的合并。
#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
在实际绘图中,不少朋友困惑于如何将传统绘图和ggplot2绘图系统在一页中实现。本文将介绍一种新方法,参考Paul Murrell编写的R Graphics Third Edition中第12章Combining Graphics Systems中的方法,在一页中多图展示3种箱线图(ggplot2,lattice和传统绘图)。实现代码:
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()