Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix the aspect ratio in ggplot?

Tags:

r

ggplot2

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?

like image 781
htorque Avatar asked Aug 14 '11 12:08

htorque


People also ask

What is aspect ratio R?

asp is a parameter of the plot() function in R Language is used to set aspect ratio of plots (Scatterplot and Barplot). Aspect ratio is defined as proportional relationship between width and height of the plot axes.

What is %>% in Ggplot?

the %>% is a pipe operator that is actually part of the dplyr library (along with the filter function) not from the ggplot2 library. To sample 1%, there is a sample_frac function in the dplyr library. It would be something like (df %>% sample_frac(0.01))

How do I make Ggplots wider?

To increase the width of axes (both X-axis and Y-axis at the same time) using ggplot2 in R, we can use theme function with axis. line argument where we can set element_line argument to a larger value.

What does GG stand for in Ggplot?

ggplot2 [library(ggplot2)] ) is a plotting library for R developed by Hadley Wickham, based on Leland Wilkinson's landmark book The Grammar of Graphics ["gg" stands for Grammar of Graphics].


4 Answers

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

like image 63
Andrie Avatar answered Oct 05 '22 05:10

Andrie


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

like image 21
a different ben Avatar answered Oct 05 '22 06:10

a different ben


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:

like image 42
Graipher Avatar answered Oct 05 '22 05:10

Graipher


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

like image 20
mikemtnbikes Avatar answered Oct 05 '22 04:10

mikemtnbikes