Why does cop_equal not work as it should?

According to this link 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, when I print it, and I see it. enter image description here

Does anyone know what the problem is?

Edit:

Without +coord_equal() , however, I can get a 1: 1 aspect ratio. However, as soon as I add the legend to the right, the 1: 1 aspect changes. Offers are simply too cumbersome to achieve the desired effect. As I said, I applied for a ticket to github / ggplot2.

+6
source share
3 answers

Could also include my comment in the answer.

What does your coord_equal(ratio = 1) mean, make sure that equal length on both axes represents the same change in units. So, 1 cm = 5 units for both axes (for example, the conversion rate is probably incorrect, but the idea is the same). Since the x axis is more variable, it will be smoothed like this. You can add the ylim parameter to coord_equal if you want the y axis to be more extended.

+8
source

After submitting the github / ggplot2 ticket. Winston helped me find a concise solution:

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

Also, apparently, some behavior changes between ggplot2 0.8 - 0.9, the original documentation is probably outdated.

+3
source

To get a plot similar to the plot of the man page , the limits of the y axis must be manually changed:

 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

+2
source

Source: https://habr.com/ru/post/925492/


All Articles