Re: [R] ggplot2, coord_equal and aspect ratio

From: hadley wickham <>
Date: Thu, 10 Jan 2008 19:35:52 -0600

On Jan 10, 2008 6:16 PM, jiho <> wrote:
> Hi everyone, Hi Hadley,
> I am a heavy user of coord_equal() in ggplot2 since most of my data is
> spatial, on x,y coordinates. Everything works. However by enforcing an
> aspect ratio of 1 for the plotting region, coord_equal() usually
> wastes a lot of space if the region of interest is not a perfect square.
> For example:
> x=runif(10)
> a=data.frame(x=x*3,y=x)
> ggplot(data=a, aes(x=x,y=y)) + geom_point() + coord_equal()
> produces a square plotting region while all the data is constrained in
> an horizontally extended rectangle. I would expect the coordinates to
> stay equal but the plot to be extended so that it fills as much as the
> plotting region as possible. It does not appear to be currently
> doable. Is it a limitation of ggplot? of the underlying grids
> graphics? Is there a workaround this?

That's not currently possible unfortunately because coord_equal has no information about the size of the plotting device. Of course, if there is specific a non-square aspect ratio that you want, you can use e.g.

qplot(x, y, data=a) + coord_equal(ratio = 2) + opts(aspect.ratio = 2)

(or in the next version of ggplot
qplot(x, y, data=a) + coord_equal(ratio = 2) where the aspect ratio is automatically set)

> x=runif(10)
> qplot(x*3, x) + coord_equal()
> produces some very strange scales. this looks like a bug to me.
> especially since:
> foo=x
> qplot(foo*3, foo) + coord_equal()
> works as expected

Yes, that's a bug that's fixed in the next version (which should be available on cran soon)

> PPS: I tried
> ggplot(data=a, aes(x=x,y=y)) + geom_point() + coord_equal() +
> scale_y_continuous(limits=c(0,1))
> which has no effect. but the side effect of enforcing the square
> domain is that:
> ggplot(data=a, aes(x=x,y=y)) + geom_point() + coord_equal() +
> scale_y_continuous(limits=c(0,0.3))
> has no effect either (i would expect to see only the points <0.3)

That seems to have been fixed in the next version too.



______________________________________________ mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.
Received on Fri 11 Jan 2008 - 01:39:18 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 11 Jan 2008 - 02:30:07 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive