Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why coord_equal doesn't work the way it should?

Tags:

r

ggplot2

According to this reference page on ggplot2, the following command should give an equal aspect ratio (1:1) of x and y.

qplot(mpg, wt, data = mtcars) + coord_equal(ratio = 1) 

However, as I type it and I am seeing this. enter image description here

Does anyone know what's the problem?

Edit:

Without +coord_equal() however, I can obtain a 1:1 aspect ratio. However, as soon as I add legend on the right, the 1:1 aspect gets changed. The suggestions provided are simply too cumbersome to achieve desired effect. As suggested, I've filed a ticket to github/ggplot2.

like image 971
Oliver Avatar asked Sep 16 '12 00:09

Oliver


Video Answer


2 Answers

Might as well turn my comment into an answer.

What your coord_equal(ratio = 1) does is make sure that the an equal length on both axis represents the same change in units. So 1cm = 5units for both axis (for example - that conversion rate probably isn't correct but the idea is the same). Since the x axis is more variable it will be scrunched like that. You can add a ylim parameter to coord_equal if you want the y-axis to be more stretched out.

like image 73
Dason Avatar answered Oct 03 '22 20:10

Dason


After github/ggplot2 ticket filing. Winston helped me to find a succinct solution:

qplot(mpg,wt,data=mtcars, shape="carb") + theme(aspect.ratio=1)

Also, there appears to be some behavioral changes between ggplot2 0.8 to 0.9, the original documentation is probably out of date.

like image 44
Oliver Avatar answered Oct 03 '22 20:10

Oliver