[英]R: ggplot2 setting the last plot in the midle with facet_wrap
您还可以考虑使用egg
包中的set_panel_size
函数,它可以让您将多个绘图的面板大小(宽度、高度)设置为绝对测量值,有关更多详细信息,请参阅此小插图。
使用 Axeman 的代码创建绘图
library(egg)
library(gridExtra)
p <- ggplot(mapping = aes(x = Day, y = Temp)) +
facet_wrap(~Month) +
geom_line()
p1 <- p %+% subset(airquality, Month < 8) + labs(x = NULL)
p2 <- p %+% subset(airquality, Month > 7)
现在在指定面板大小后使用grid.arrange
排列图
grid.arrange(grobs = lapply(
list(p1, p2),
set_panel_size,
width = unit(5, "cm"),
height = unit(4, "cm")
))
实现所需结果的另一个选择是ggh4x
package ,它通过ggh4x::facet_manual
通过design
参数为 position 增加了一些灵活性:
library(ggplot2)
library(ggh4x)
base <- ggplot(airquality, aes(x = Day, y = Temp)) +
geom_line()
design <- c(
"
AABBCC
#DDEE#
"
)
base + ggh4x::facet_manual(~Month, design = design)
design <- c(
"
AABBCC
DDDEEE
"
)
base + ggh4x::facet_manual(~Month, design = design)
以防万一有人想知道如何在网格中从头开始...
library(ggplot2)
library(dplyr)
library(grid)
vps <- c("top_left", "top_mid", "top_right", "bottom_left", "bottom_right")
main_vp <- vpTree(viewport(name = "main"), vpList(
viewport(x = 1/6, y = 0.75, width = 1/3, height = 0.5, name = vps[1]),
viewport(x = 3/6, y = 0.75, width = 1/3, height = 0.5, name = vps[2]),
viewport(x = 5/6, y = 0.75, width = 1/3, height = 0.5, name = vps[3]),
viewport(x = 1/3, y = 0.25, width = 1/3, height = 0.5, name = vps[4]),
viewport(x = 2/3, y = 0.25, width = 1/3, height = 0.5, name = vps[5])))
grid.newpage()
pushViewport(main_vp)
plots <- lapply(1:5, function(i){
seekViewport(vps[i])
invisible(grid.draw(ggplotGrob(
ggplot(filter(airquality, Month == levels(as.factor(airquality$Month))[i]),
mapping = aes(Day, Temp)) +
geom_line() +
facet_grid(.~Month)
)))})
由reprex 包(v0.3.0) 于 2020 年 7 月 3 日创建
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.