2

when plotting a boxplot in R, we can remove/hide outliers by outlier.shape=NA, but this does not change the limits of y-axis. so, if there is an outlier of size 1000, and the rest of the plot lies below 50, there will be a huge empty space in the plot, pushing the boxes to the lower part and make them look small.

Is there anyway to shrink the y-axis limits automatically?

Thanks

OverFlow Police
  • 861
  • 6
  • 23

2 Answers2

0

You can shrink the axis, x or y by specifying xlim or ylim with lower and upper limits inside the brackets in ggplot2

example with mtcars :

ggplot(mtcars, aes(x = factor(am), y=mpg)) + 
geom_boxplot() + 
ylim(NA, 32) # case you want to change the upper limits (e.g.)

Furthermore, you may want to know how outlier are considered in ggplot. According how your data are distributed, you could replace the upper limit number in ylim with something like quantile(mtcars$mpg, 0.95) if you want to remove point after the 95th percentile without setting a value manually.

tom
  • 725
  • 4
  • 17
0

The solution here works: Ignore outliers in ggplot2 boxplot

coord_cartesian(ylim = ylim1*1.05)

OverFlow Police
  • 861
  • 6
  • 23