Oliver
Oliver

Reputation: 3682

Why coord_equal doesn't work the way it should?

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.

Upvotes: 7

Views: 6484

Answers (3)

Oliver
Oliver

Reputation: 3682

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.

Upvotes: 5

Sven Hohenstein
Sven Hohenstein

Reputation: 81683

To get a plot similar to the one on the reference page, the limits of the y-axis have to be changed manually:

library(ggplot2)

r_wt <- range(with(mtcars, wt))
r_mpg <- range(with(mtcars, mpg))
cent <- mean(r_wt)
ylimits <- cent + c(-1, +1) * diff(r_mpg)/2

qplot(mpg, wt, data = mtcars) + coord_cartesian(ylim = ylimits)

enter image description here

Upvotes: 2

Dason
Dason

Reputation: 61903

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.

Upvotes: 9

Related Questions