如何修复ggplot中的纵横比?

发布于 2024-11-29 18:43:04 字数 302 浏览 0 评论 0原文

我正在尝试调整绘图的大小以适合我的文档,但我很难将绘制的图表变成正方形。

示例:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

无力看清 2024-12-06 18:43:04

在 ggplot 中,保留绘图纵横比的机制是向绘图添加一个 coord_fixed() 层。无论实际边界框的形状如何,这都将保留绘图本身的纵横比。

(我还建议您使用 ggsave 将结果图保存为 pdf/png/etc,而不是 pdf(); print(p); dev.off() 序列。)

library(ggplot2)
df <- data.frame(
    x = runif(100, 0, 5),
    y = runif(100, 0, 5))

ggplot(df, aes(x=x, y=y)) + geom_point() + coord_fixed()

在此处输入图像描述

In ggplot the mechanism to preserve the aspect ratio of your plot is to add a coord_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 the pdf(); print(p); dev.off() sequence.)

library(ggplot2)
df <- data.frame(
    x = runif(100, 0, 5),
    y = runif(100, 0, 5))

ggplot(df, aes(x=x, y=y)) + geom_point() + coord_fixed()

enter image description here

梦魇绽荼蘼 2024-12-06 18:43:04

要确保特定的宽高比(例如正方形),请使用theme(aspect.ratio=1)

Andrie 的答案并没有给出完整的情况,因为该示例提供了可能不自然的数据,其中 x 的范围等于 y 的范围。然而,如果数据是:

df <- data.frame(
  x = runif(100, 0, 50),
  y = runif(100, 0, 5))
ggplot(df, aes(x=x, y=y)) + geom_point() + coord_fixed()

那么绘图将如下所示:

在此处输入图像描述

coord_fixed() 函数还有一个调整轴比例的参数:

ratio 纵横比,表示为 y / x

以便绘图可以与以下内容成正方形:

ggplot(df, aes(x=x, y=y)) + geom_point() + coord_fixed(ratio=10)

enter image这里的描述

但是您需要根据变量或绘图区域的限制来调整它(并非所有限制都可以很好地被整数整除,就像这些示例一样)。

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:

df <- data.frame(
  x = runif(100, 0, 50),
  y = runif(100, 0, 5))
ggplot(df, aes(x=x, y=y)) + geom_point() + coord_fixed()

then the plot would look like this:

enter image description here

The coord_fixed() function also has an argument to adjust the ratio of axes:

ratio aspect ratio, expressed as y / x

So that the plot could be made square with:

ggplot(df, aes(x=x, y=y)) + geom_point() + coord_fixed(ratio=10)

enter image description here

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).

就像说晚安 2024-12-06 18:43:04

为了完整起见:
如果您想考虑非常不同的轴限制:

df <- data.frame(
  x = runif(100, 0, 5000),
  y = runif(100, 0, 5))
ratio.display <- 4/3
ratio.values <- (max(df$x)-min(df$x))/(max(df$y)-min(df$y))
plot <- ggplot(df, aes(x=x, y=y)) + geom_point()
plot + coord_fixed(ratio.values / ratio.display)

导致:

For completeness sake:
If you want to take very different axis limits into account:

df <- data.frame(
  x = runif(100, 0, 5000),
  y = runif(100, 0, 5))
ratio.display <- 4/3
ratio.values <- (max(df$x)-min(df$x))/(max(df$y)-min(df$y))
plot <- ggplot(df, aes(x=x, y=y)) + geom_point()
plot + coord_fixed(ratio.values / ratio.display)

Resulting in:

蔚蓝源自深海 2024-12-06 18:43:04

基于 baptiste 的建议和 Graipher答案,因为它优雅且有用。

df <- data.frame(
  x = runif(100, 0, 5000),
  y = runif(100, 0, 5))
aspect.ratio <- 4/3
ratio.values <- (max(df$x)-min(df$x))/(max(df$y)-min(df$y))
plot <- ggplot(df, aes(x=x, y=y)) + geom_point() + theme(aspect.ratio=4/3)

用 1 绘图:1 纵横比

Based on baptiste's suggestion and Graipher's answer because it is elegant and useful.

df <- data.frame(
  x = runif(100, 0, 5000),
  y = runif(100, 0, 5))
aspect.ratio <- 4/3
ratio.values <- (max(df$x)-min(df$x))/(max(df$y)-min(df$y))
plot <- ggplot(df, aes(x=x, y=y)) + geom_point() + theme(aspect.ratio=4/3)

Plot with 1:1 aspect ratio

对你的占有欲 2024-12-06 18:43:04

我看到这篇文章并想添加更多信息。如果您打算将图像插入 RMD 文档中,则可以从 r 块中处理 aspect.ratio 。根据您的布局,有 3 种方法可以更改图像的纵横比(取决于您的工作流程)。

#1.创建图

myPlot <- ggplot(data = iris,
aes(x=Species, y = Sepal.Length)) +
geom_point() +
theme(aspect.ratio = 6.5/11)

#2 时。使用 ggsave 保存图像时

ggsave("images/plot1.png", myPlot, width=11, height=6.5, dpi=400)

#3。在 rmd 文档本身中。如果您计划使用 officedown(并创建 docx 作为文档输出),那么我发现 6.5/11 的宽高比与 9.4 的 fig.width 相结合最好使用横向页面。

  <!---BLOCK_LANDSCAPE_START--->
    ```{r fig.id="my-fig", fig.cap="", fig.width=9.4, fig.asp = 6.5/11}
    myPlot
    ```
 <!---BLOCK_LANDSCAPE_STOP--->

应该注意的是,如果您计划将 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

myPlot <- ggplot(data = iris,
aes(x=Species, y = Sepal.Length)) +
geom_point() +
theme(aspect.ratio = 6.5/11)

#2. While saving the image using ggsave

ggsave("images/plot1.png", myPlot, width=11, height=6.5, dpi=400)

#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 a fig.width of 9.4 is the best using a landscaped page.

  <!---BLOCK_LANDSCAPE_START--->
    ```{r fig.id="my-fig", fig.cap="", fig.width=9.4, fig.asp = 6.5/11}
    myPlot
    ```
 <!---BLOCK_LANDSCAPE_STOP--->

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.

错爱 2024-12-06 18:43:04

尝试添加

a_r <- 2.5
theme(aspect.ratio = a_r) 

这是示例:

ggplot(corr_df_0, aes(x = corr_df_0_sp, y = corr_df_0_sc))+
theme(aspect.ratio=2.5) 

Try adding

a_r <- 2.5
theme(aspect.ratio = a_r) 

This is the example:

ggplot(corr_df_0, aes(x = corr_df_0_sp, y = corr_df_0_sc))+
theme(aspect.ratio=2.5) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文