且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在ggplot2中明确设置面板尺寸(不仅是绘图尺寸)

更新时间:2021-12-31 23:09:36

您可以从set_panel_size()函数. rel ="nofollow noreferrer">鸡蛋包

You can use set_panel_size() function from the egg package

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)

reprex软件包(v0.2.1.9000)创建于2018-11-13

Created on 2018-11-13 by the reprex package (v0.2.1.9000)