Skip to contents

Function to remove boxplot outliers and hover info from a plotly::ggplotly() object. Only boxplot layers will be altered. Works also in faceted ggplotly figures. This is needed since boxplot outliers will cannot be removed in interactive plotly figures due to a bug. See also

Usage

remove_boxplot_outliers(fig)

Arguments

fig

A plotly object created with plotly::ggplotly().

Value

The same plotly::ggplotly() object, now with outliers in boxplots deleted.

Examples

library(ggplot2)
library(plotly)
#> 
#> Attaching package: ‘plotly’
#> The following object is masked from ‘package:ggplot2’:
#> 
#>     last_plot
#> The following object is masked from ‘package:stats’:
#> 
#>     filter
#> The following object is masked from ‘package:graphics’:
#> 
#>     layout
set.seed(2023) # for reproducible jitter
fig <- ggplotly({
  ggplot(iris, aes(factor(1), Petal.Width)) + geom_boxplot(outlier.shape = NA) + 
    geom_jitter(col = "red") + 
    facet_wrap(~Species)
 })
 # Outliers are visible in black in the plotly figure, even though they should not:
fig
#The function `remove_boxplot_outliers()` corrects for this: remove_boxplot_outliers(ggplotly(fig))