69

I can force ggplot2 scatter plot to be square shaped with the same x and y scaling using xlim() and ylim(), but it needs manual calculation of the limits. Is there any more convenient way of doing it?

By square shape I mean two requirements:

  1. The same scale on x and y axis.
  2. The equal length of x and y axis.
8
  • 2
    By "square shaped" do you mean you want the length of one unit in the x direction to be the same as in the y direction (meaning if x goes from 0 to 5 and y goes from 0 to 6 the y dimension will be one unit longer), or do you mean that you want the length of the entire x axis to be the same as the y axis (so in the previous example, each unit of y is shorter than each unit of x but the entire graph looks square)? Commented Nov 18, 2012 at 23:41
  • 1
    @baptiste: I think you should post that as an answer. It appears to be the ggplot analogue of asp=1 in base plotting.
    – IRTFM
    Commented Nov 18, 2012 at 23:44
  • @DWin It seems none of the proposed answers make the plot square shaped, they all make the x and y scales the same.
    – Ali
    Commented Nov 19, 2012 at 15:24
  • 17
    if the shape matters, go with theme(aspect.ratio=1)
    – baptiste
    Commented Nov 19, 2012 at 18:37
  • 1
    but to answer your question, I don't think there is a function to do that in ggplot2. You could work with expand_limits, but I believe one way or another you'll have to compute min and max of your data manually.
    – baptiste
    Commented Nov 19, 2012 at 18:41

6 Answers 6

81

Note: for a square shape (irrespective of the data being plotted),

ggplot() + theme(aspect.ratio=1)

enter image description here

1
  • 3
    This forces the plot background area to be square, not (necessarily) the plot data area. In addition, ggsave may not (almost surely will not) output a square image, but will pad it with white, so that if your background color is white, your saved graph will not appear to be square (and if your background is colored, it will be surrounded by white padding). Depending on your purpose, this option ought to used together with other options. You can always tweak width and height options inside ggsave.
    – PatrickT
    Commented Feb 9, 2018 at 5:46
75

If you want to make the distance scale points the same, then use coord_fixed():

p <- ggplot(...)
p <- p + coord_fixed() # ratio parameter defaults to 1 i.e. y / x = 1

If you want to ensure that the resulting plot is square then you would also need to specify the x and y limits to be the same (or at least have the same range). xlim and ylim are both arguments to coord_fixed. So you could do this manually using those arguments. Or you could use a function to extract out limits from the data.

1
  • 13
    Thanks, it makes the x and y axis to be the same scale, but does not force the plot to be square shaped -i.e. the length of x and y axis can be different
    – Ali
    Commented Nov 19, 2012 at 14:28
9

Probably the ugliest code you'll see today, but it does the trick.

The ranges of your x and y axes are accessible from ggplot_build:

r<-max(abs(ggplot_build(your_plot)$panel$ranges[[1]]$x.range))
s<-max(abs(ggplot_build(your_plot)$panel$ranges[[1]]$y.range))
t<-round(max(r,s),1)
your_plot<-your_plot+coord_equal(xlim=c(-t,t),ylim=c(-t,t))
7

Although an older post, this is still a top hit on google for me.

This function in the tune package seems to accomplish this now:

p <- 
  mtcars %>% 
  ggplot(aes(drat, wt)) + 
  geom_abline(slope = 1, intercept = 0) +
  geom_point()
p

enter image description here

p + tune::coord_obs_pred()

enter image description here

5

All the solutions provided in the previous answers do not work for my R version: R version 3.6.1.

ggplot_build(pot)$panel$ranges[[1]]$x.range # return NULL value

The solution mentioned by @Gerhard Burger in the linked URL works for my case:

r<-max(abs(layer_scales(plt)$x$range$range))
s<-max(abs(layer_scales(plt)$y$range$range))
t<-round(max(r,s),1)
plt<-plt+coord_equal(xlim=c(0,t),ylim=c(0,t))
2

Building on Ramons answer, this function works nicely for me and I consider it not as ugly, since one can hide the function definition...

squarePlot <- function(plt){
    return(plt+coord_equal()+
            expand_limits(x=ggplot_build(plt)$panel$ranges[[1]]$y.range,
                          y=ggplot_build(plt)$panel$ranges[[1]]$x.range))
}

just wrapping Ramon's code in a function didn't work out for me because the t variable is defined in the "wrong" environment.

2
  • I like the idea, but this was not working for my ggplot object. The closest I got was inside ggplot_build(qq)$plot$scales$scales, but it still wasn't working. So I ended up computing the limits manually and scaling by 1.3...
    – PatrickT
    Commented Nov 7, 2017 at 19:37
  • 1
    The ggplot_build changes all the time, I think at the moment this is the preferred method Commented Oct 10, 2018 at 20:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.