0

I just noticed how ggplot is removing datapoints when the scale_y_continuous is limited so that certain values are not included. Example:

library("ggplot2")
exdf <- mtcars

ggp <- ggplot(data=exdf, mapping=aes(x=cyl, y=disp)) + 
    stat_summary(fun.y="mean", geom="point", size=4) + 
    scale_y_continuous(limits=c(1, 400))
ggp 

#compare to: 
range(exdf$disp)
ggp <- ggplot(data=exdf, mapping=aes(x=cyl, y=disp)) + 
    stat_summary(fun.y="mean", geom="point", size=4) + 
    scale_y_continuous(limits=c(1, 500))
ggp 

All three dots are displayed and are not limited by the range, but the underlying data-range is limited. Note the messages about "rows containing non-finite values". I find this behavior odd and wonder if there might be a workaround?

vanao veneri
  • 970
  • 2
  • 12
  • 31
  • What specifically do you mean by _workaround_? Please elaborate on your desired behavior. – Eric Fail Jan 22 '16 at 15:24
  • You could use coord_cartesian to limit plot rather than scale_y_continuous limits. – Nick F Jan 22 '16 at 15:25
  • I want the values to be NOT exclude in the calculation of the stat_sumary, at the same time, I want to limit my y-axis at will. – vanao veneri Jan 22 '16 at 15:25
  • I think ggplot is forcing you to do the right thing here: excluding points from the plot but including them in the summary is … wrong, and in fact dishonest. There may be situations where it’s legitimate (can’t think of any) but in general it’s not something you should do. – Konrad Rudolph Jan 22 '16 at 15:28
  • @Nick F: That worked! Thank you. – vanao veneri Jan 22 '16 at 15:29
  • @Konrad: Not sure if I catch your drift here, do you mean that limiting an axis in a plot to a data-range that this narrower than the actual databasis is deceiving? Even if the plot containts something like errorbars? – vanao veneri Jan 22 '16 at 15:31
  • @vanaoveneri In general yes, that’s what I mean. Even if error bars are given the actual points corresponding to outliers are informative, and leaving them out may give the false impression that no outliers exist. – Konrad Rudolph Jan 22 '16 at 15:43
  • Fair point. I'll keep that in mind. – vanao veneri Jan 22 '16 at 15:48

0 Answers0