|||
R 语言中一页多图的设置也很方便,可以使用 grid 包中的 viewport() 函数。示例如下:
library(ggplot2)
x <- 1:20
fun1 <- function(x) {
3 + 2 * log(x)
}
fun2 <- function(x) {
3 - 2 * log(x)
}
a <- ggplot(NULL, aes(x = x)) + stat_function(fun = fun1, n = 20, shape = 1, size = 3,
geom = c("point")) +
geom_line(stat = "function", fun = fun1) +
theme_bw() + theme(panel.grid = element_blank())
b <- ggplot(NULL, aes(x = x)) + stat_function(fun = fun2, n = 20, shape = 1, size = 3,
geom = c("point")) +
geom_line(stat = "function", fun = fun2) +
theme_bw() + theme(panel.grid = element_blank())
library(grid)
grid.newpage() #新建一个空白页
pushViewport(viewport(layout = grid.layout(nrow = 1, ncol = 2))) #设置好页面布局,1 行 2 列
print(a, vp = viewport(layout.pos.row = 1, layout.pos.col = 1)) #将 a 图放在第 1 行第 1 列
print(b, vp = viewport(layout.pos.row = 1, layout.pos.col = 2)) #将 b 图放在第 1 行第 2 列
图形显示如下:
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-11-23 05:13
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社