0

I have a for loop to create box plots for a couple dozen analytes grouped by location:

for(parameter in analyte_list) {
  print(parameter)
  df_analytes <- df %>% filter(Analyte == parameter)
  df_box <- ggplot(df_analytes, aes(y = Result, x = MONTH, fill = Location)) +
    geom_boxplot() +
    ylab(paste(unique(df_analytes$Units))) +
    xlab("Month") +
    ggtitle(paste("Monthly", parameter))
  ggsave(paste0("Plots/Box/Box_", parameter, ".png"), df_box)
  print(df_box)
}

Which produces boxplots that look like this: WaterTemp and Cl

The first plot looks fine as is but the second is difficult to read due to a single high outlier. Is there a way to define ylim automatically (perhaps according to the stats for each analyte or some determined value based on the 75th percentile) for better visualization without having to remove outliers?

mika
  • 13
  • 4
  • Hi mika. To the best of my knowledge there is no such automatic mechanism out of the box. Maybe there is a package to achieve this. However, you could e.g. write a custom function to achieve this, i.e. a function which computes the stats which could then be used to set `ylim`. – stefan Jan 15 '21 at 06:52
  • You could set the `ylim` as `min` and the max as some quantile. Something like `ylim(min(df$Result), quantile(df%Result, probs=0.75))` – George Jan 15 '21 at 08:16
  • Check this (https://stackoverflow.com/questions/5677885/ignore-outliers-in-ggplot2-boxplot) out for a much more elegant solution. Basically, you calculate the whiskers coord and zoom out a bit from that. Note: the use of `coord_cartesian` rather than `ylim` – George Jan 15 '21 at 08:25
  • @stefan Thank you for your comment! I will certainly give this a try; I still struggle with custom functions so it might be good practice. – mika Jan 27 '21 at 06:23
  • @George This looks very promising - I am just returning to this after getting bogged down with some other problems on this project, so I will give it a go and update this post accordingly. Thank you so much! – mika Jan 27 '21 at 06:27

0 Answers0