[R] ggplot2, coord_equal and aspect ratio

hadley wickham h.wickham at gmail.com
Fri Jan 11 02:35:52 CET 2008


On Jan 10, 2008 6:16 PM, jiho <jo.irisson at gmail.com> 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.

Hadley

-- 
http://had.co.nz/




More information about the R-help mailing list