精确设置ggsave的大小 [英] Set the size of ggsave exactly

查看:1217
本文介绍了精确设置ggsave的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

R 问题。

我对宽度高度 dpi 单位

以下两个大小为何不同?

Why the following two size different?

ggsave(filename = "foo.png",ggplot(mtcars, aes(x=wt, y=mpg)) +
    geom_point(size=2, shape=23),width = 5, height = 4, dpi = 300, units = "in", device='png')

ggsave(filename = "foo.png",ggplot(mtcars, aes(x=wt, y=mpg)) +
           geom_point(size=2, shape=23),width = 5, height = 4, dpi = 72, units = "in", device='png')

我将图片的尺寸都设置为5(英寸)* 4(英寸)。但是,为什么更改 dpi 时,尺寸会改变?

I set both of the picture's size 5 (inches) * 4 (inches). But why when I change the dpi, the size changes?

如何理解高度宽度之间的关系,单位 dpi

How to understand the relationship between height, width, unit and dpi?

还是如何翻译

推荐答案

要理解DPI为什么如此重要,请考虑这两个图:

To understand why the DPI is important, consider these two plots:

ggsave(filename = "foo300.png", ggplot(mtcars, aes(x=wt, y=mpg)) +
           geom_point(size=2, shape=23) + theme_bw(base_size = 10),
       width = 5, height = 4, dpi = 300, units = "in", device='png')
ggsave(filename = "foo150.png", ggplot(mtcars, aes(x=wt, y=mpg)) +
           geom_point(size=2, shape=23) + theme_bw(base_size = 10),
       width = 10, height = 8, dpi = 150, units = "in", device='png')

生成的文件具有相同的像素大小,但每个文件中的字体大小不同。如果将它们放置在与它们的 ggsave()调用物理尺寸相同的页面上,则字体大小将是正确的(即在中为10)。 ggsave()调用)。但是,如果将它们以错误的物理尺寸放置在页面上,则字体大小将不会为10。要在增加DPI的同时保持相同的物理尺寸和字体大小,必须增加图像中的像素数量。

The resulting files have the same pixel dimensions, but the font size in each is different. If you place them on a page with the same physical size as their ggsave() calls, the font size will be correct (i.e. 10 as in the ggsave() call). But if you put them on a page at the wrong physical size, the font size won't be 10. To maintain the same physical size and font size while increasing DPI, you have to increase the number of pixels in the image.

这篇关于精确设置ggsave的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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