gpt4 book ai didi

r - 将 ggsave 中的 Canvas 大小设置为固定宽度,同时保持图的纵横比

转载 作者:行者123 更新时间:2023-12-04 17:40:06 27 4
gpt4 key购买 nike

长话短说,我为我的学生制作了 YouTube 视频,并制作了很多图片。到目前为止,我已经将图像导入 Premiere,调整它们的大小,然后在 ggplot 图形后面放置一个单独的白色背景图像。这有点乏味,我希望能够直接从 R 导出到适当的大小 (1920x1080 p),这样我就不必调整大小和添加背景图像。

我能够调整图形的大小,但现在它拉伸(stretch)了它:

enter image description here

我希望它看起来像这样(我在图像和 Canvas 周围放置了方框,以使其更清楚绘图结束和背景 Canvas 开始的位置):

enter image description here

我知道我可以指定外部边距(参见 How can I control the canvas size in ggplot? ),但这需要我为每个地 block 设置外部边距;有时我的地 block 比高度宽,有时我的地 block 比宽度高。 ggsave 似乎尊重纵横比并最大化一个维度或另一个维度。

那么如何在保持正确纵横比的同时设置外边距呢?

最佳答案

您可以将面板大小设置为特定值,

library(egg)
library(ggplot2)
library(grid)

p <- ggplot() + labs(x = 'x title', y = 'y title')

ggsave('notset.png', p, width = 6.4, height = 3.6, units = 'in', dpi = 300)

ggsave('set.png', egg::set_panel_size(p, width=unit(4, "in"), height=unit(3, "in")),
width = 6.4, height = 3.6, units = 'in', dpi = 300)

关于r - 将 ggsave 中的 Canvas 大小设置为固定宽度,同时保持图的纵横比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54892086/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com