如何修复ggplot中的纵横比?
我正在尝试调整绘图的大小以适合我的文档,但我很难将绘制的图表变成正方形。
示例:
pdf(file = "./out.pdf", width = 5, height = 5)
p <- ggplot(mydata, aes(x = col1, y = col2))
print(p)
aux <- dev.off()
虽然 x 和 y 的限制相同,但结果中的图不是正方形。我猜想 R 将封闭面板设为 5x5",但并不关心实际图表大小。
我如何取消压缩我的图表?
I'm trying to resize a plot to fit into my document, but I'm having difficulties getting the plotted diagram do be a square.
Example:
pdf(file = "./out.pdf", width = 5, height = 5)
p <- ggplot(mydata, aes(x = col1, y = col2))
print(p)
aux <- dev.off()
Although the limits for x and y are the same, the plot in the result isn't square. I guess that R makes the enclosing panel 5x5" but doesn't care about the actual diagram size.
How can I unsquash my diagrams?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在 ggplot 中,保留绘图纵横比的机制是向绘图添加一个 coord_fixed() 层。无论实际边界框的形状如何,这都将保留绘图本身的纵横比。
(我还建议您使用 ggsave 将结果图保存为 pdf/png/etc,而不是 pdf(); print(p); dev.off() 序列。)
In
ggplot
the mechanism to preserve the aspect ratio of your plot is to add acoord_fixed()
layer to the plot. This will preserve the aspect ratio of the plot itself, regardless of the shape of the actual bounding box.(I also suggest you use
ggsave
to save your resulting plot to pdf/png/etc, rather than thepdf(); print(p); dev.off()
sequence.)要确保特定的宽高比(例如正方形),请使用
theme(aspect.ratio=1)
。Andrie 的答案并没有给出完整的情况,因为该示例提供了可能不自然的数据,其中 x 的范围等于 y 的范围。然而,如果数据是:
那么绘图将如下所示:
coord_fixed() 函数还有一个调整轴比例的参数:
以便绘图可以与以下内容成正方形:
但是您需要根据变量或绘图区域的限制来调整它(并非所有限制都可以很好地被整数整除,就像这些示例一样)。
To ensure a particular aspect ratio, e.g. for square, use
theme(aspect.ratio=1)
.Andrie's answer doesn't give the full picture, as the example provides perhaps unnatural data where range of x equals the range of y. If however the data were:
then the plot would look like this:
The coord_fixed() function also has an argument to adjust the ratio of axes:
So that the plot could be made square with:
But you need to adjust this with the limits of the variables or plot area (not all limits are nicely divisible by whole numbers like these examples).
为了完整起见:
如果您想考虑非常不同的轴限制:
导致:
For completeness sake:
If you want to take very different axis limits into account:
Resulting in:
基于 baptiste 的建议和 Graipher 的答案,因为它优雅且有用。
Based on baptiste's suggestion and Graipher's answer because it is elegant and useful.
我看到这篇文章并想添加更多信息。如果您打算将图像插入 RMD 文档中,则可以从 r 块中处理
aspect.ratio
。根据您的布局,有 3 种方法可以更改图像的纵横比(取决于您的工作流程)。#1.创建图
#2 时。使用 ggsave 保存图像时
#3。在 rmd 文档本身中。如果您计划使用
officedown
(并创建 docx 作为文档输出),那么我发现 6.5/11 的宽高比与 9.4 的fig.width
相结合最好使用横向页面。应该注意的是,如果您计划将 ggplot 插入到 Rmd 文档中,您可以跳过 #1 和 #2,仅使用选项 #3。
I fell on this post and wanted to add some more information. If you plan on inserting your image into an RMD document then
aspect.ratio
can be handled from the r chunk. Depending on your layout there are 3 ways to change the aspect ratio of the image (depending on your workflow).#1. While creating the figure
#2. While saving the image using
ggsave
#3. In the rmd document itself. If you plan on using
officedown
(and creating a docx as your document output), then I have found the 6.5/11 aspect ratio in combination with afig.width
of 9.4 is the best using a landscaped page.It should be noted that if you plan on inserting the ggplot into your Rmd document you can skip #1 and #2 and only use option #3.
尝试添加
这是示例:
Try adding
This is the example: