12

How can I adjust my Y axis in order to ignore outliers, like in this post, but in a more challenging case where I have 4 boxplots and a "free faceting" layout?

p <- ggplot(molten.DF,aes(x=class,y=SOC,fill=class)) + geom_boxplot() + facet_grid(layer~.,scales="free",space="free")

As you can see on my figure, considering outliers in the Y axis range make the boxes more difficult to read. No importance if some outliers are still visible in the result, but I would like to really focus on the boxes!

boxplot with free scale, all outliers are (unfortuately) visibles

Community
  • 1
  • 1
fstevens
  • 1,287
  • 1
  • 17
  • 28

1 Answers1

2

It obviously depends on what you consider an outlier. If it's possible for you to calculate it, you can set your ylim at this value an let those points out of the chart.

For example, if you assume that the upper and lower limits are Q3 + 1.5 IQR and Q1 - 1.5 IQR, and this is the way boxplots usually have its outliers limit calculated, you would have:

upper.fence <- quantile(x)[4] + 1.5*IQR(x)
lower.fence <- quantile(x)[2] - 1.5*IQR(x)

Then you can use these limits as the y range of values:

my.ggplot + coord_cartesian(ylim=c(lower.fence, upper.fence))

The procedure you use to get your upper and lower limits can change, but the use of the limits is the same.

João Daniel
  • 8,696
  • 11
  • 41
  • 65
  • 2
    I think it's safe to assume OP wants outliers as defined by ggplot: http://docs.ggplot2.org/current/geom_boxplot.html (Details section). Also, the `ylim` approach only works for a single boxplot, since outliers of some boxplots will be inside the fence of others. – Max Ghenis Mar 26 '16 at 00:01