开发者

How to fix the aspect ratio in ggplot?

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?


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

How to fix the aspect ratio in ggplot?


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:

How to fix the aspect ratio in ggplot?

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)

How to fix the aspect ratio in ggplot?

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:

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:

How to fix the aspect ratio in ggplot?


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)

How to fix the aspect ratio in ggplot?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜