在ggplot2中明确设置面板尺寸(不仅是绘图尺寸) [英] Explicitly set panel size (not just plot size) in ggplot2

查看:65
本文介绍了在ggplot2中明确设置面板尺寸(不仅是绘图尺寸)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在ggplot中显式设置面板尺寸(即灰色网格面板)?我想象(但找不到)有一些ggplot扩展名,允许类似panel.width = unit(3, "in"), panel.height = unit(4, "in")的参数.

Is it possible to explicitly set the panel size (i.e., the gray grid panel) in a ggplot? I imagine (but can't find) that there is some ggplot extension that allows for arguments that resemble panel.width = unit(3, "in"), panel.height = unit(4, "in").

我已经看到了用于设置整个图的大小或使用egg包获取多个图以对齐的解决方案.但是没有什么可以让我明确设置面板的大小.

I have seen solutions for setting the size of the entire plot, or of getting multiple plots to align using the egg package. But nothing that would let me explicitly set the size of the panel.

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

推荐答案

您可以从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)

这篇关于在ggplot2中明确设置面板尺寸(不仅是绘图尺寸)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆