[英]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.