是否可以在ggplot中明确设置面板大小(即灰色网格面板)?我想(但是找不到)有一些ggplot扩展允许类似的参数panel.width = unit(3, "in"), panel.height = unit(4, "in").
我已经看到了设置整个绘图大小的解决方案,或者使用egg包来获得多个绘图对齐的解决方案.但没有什么可以让我明确设置面板的大小.
library(dplyr)
library(ggplot2)
library(tibble)
ds_mt <- mtcars %>% rownames_to_column("model")
mt_short <- ds_mt %>% arrange(nchar(model)) %>% slice(1:4)
mt_long <- ds_mt %>% arrange(-nchar(model)) %>% slice(1:4)
p_short <-
mt_short %>%
ggplot(aes(x = model, y = mpg)) +
geom_col() +
coord_flip()
p_short
Run Code Online (Sandbox Code Playgroud)
teu*_*and 19
ggh4x 包具有与其他答案中提供的 Egg 解决方案类似的功能。一个稍微方便的地方是,使用该函数后,该图仍然是有效的 ggplot,因此它可以使用,ggsave()并且可以在之后添加其他图层。(免责声明:我写了 ggh4x)
library(dplyr)
library(ggplot2)
library(tibble)
library(ggh4x)
ds_mt <- mtcars %>% rownames_to_column("model")
mt_short <- ds_mt %>% arrange(nchar(model)) %>% slice(1:4)
mt_long <- ds_mt %>% arrange(-nchar(model)) %>% slice(1:4)
mt_short %>%
ggplot(aes(x = model, y = mpg)) +
geom_col() +
coord_flip() +
force_panelsizes(rows = unit(4, "in"),
cols = unit(3, "in"))
Run Code Online (Sandbox Code Playgroud)

由reprex 包于 2021 年 4 月 21 日创建(v1.0.0)
您可以使用egg包中的set_panel_size()函数
library(tibble)
library(dplyr)
library(ggplot2)
ds_mt <- mtcars %>% rownames_to_column("model")
mt_short <- ds_mt %>% arrange(nchar(model)) %>% slice(1:4)
mt_long <- ds_mt %>% arrange(-nchar(model)) %>% slice(1:4)
p_short <-
mt_short %>%
ggplot(aes(x = model, y = mpg)) +
geom_col() +
coord_flip()
library(egg)
library(grid)
p_fixed <- set_panel_size(p_short,
width = unit(10, "cm"),
height = unit(4, "in"))
grid.newpage()
grid.draw(p_fixed)
Run Code Online (Sandbox Code Playgroud)

由reprex 包(v0.2.1.9000)于 2018 年 11 月 13 日创建