简体   繁体   中英

Unexpected plot using xlim() and facet_wrap(…, scales=“free_y” )

I have a problem that can be reproduced using the mt data set:

require(ggplot2)
mt <- ggplot(mtcars, aes(mpg, wt, colour = factor(cyl))) + geom_point()
mt + facet_wrap(~ cyl, scales = "free_y")
mt + xlim(c(15,30)) + facet_wrap(~ cyl, scales = "free_y")

In the second plot, why does the y axis of the 3rd cell ("8") go up up to 5.5 if there are no points there? How can I get that axis to span to the actual range of the displayed points (ie, something like from 3 to 4.25)? Are not all points outside the x limits of c(15,30) being set to NA by xlim()?

There are some points there. You just cut them off. To get better scales, you may want to filter those points out first:

mt <- mtcars %>% 
  filter(mpg>=15, mpg<=30) %>% 
  ggplot(aes(mpg, wt, colour = factor(cyl))) + geom_point()
mt + facet_wrap(~ cyl, scales = "free_y")
mt + xlim(c(15,30)) + facet_wrap(~cyl, scales = "free_y")

You get: 在此处输入图像描述

I have understood the issue thanks to a recent discussion in ggplot google list 1

The question is that the result of xlim() is not really setting points (which imply both x and y coordinates) to NA, but only the x-coordinate is set to NA: "The corresponding y values are still available and hence used to calculate the range of y axis."

I have to add though that this behavior of xlim() is very counter-intuitive, if not illogical. If the user sets xlim(c(15,30), it is because he/she does not want to see points within that range, so automatic y limits should adapt in accordance. Or at least this should be an option, sort of na.rm=TRUE.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM