Jorge Kageyama
Jorge Kageyama

Reputation: 435

Removing outliers from boxplot and plotly

I am trying to create a plotly boxplot in R that doesnt show the outliers, and I found this link in the official page of plotly: https://plot.ly/ggplot2/box-plots/#outliers

library(plotly)
set.seed(123)

df <- diamonds[sample(1:nrow(diamonds), size = 1000),]

p <- ggplot(df, aes(cut, price, fill = cut)) + 
geom_boxplot(outlier.shape = NA) + 
ggtitle("Ignore outliers in ggplot2")

# Need to modify the plotly object and make outlier points have opacity equal 
to 0
p <- plotly_build(p)

p$data <- lapply(p$data, FUN = function(x){
 x$marker = list(opacity = 0)
 return(x)
})


# Create a shareable link to your chart
# Set up API credentials: https://plot.ly/r/getting-started
chart_link = plotly_POST(p, filename="geom_boxplot/outliers")
chart_link

The problem is that in their webpage and in my console, outliers are still being displayed. enter image description here Is this some kind of bug?

Upvotes: 9

Views: 7398

Answers (1)

Artem Sokolov
Artem Sokolov

Reputation: 13691

Seems like a typo. Maybe the example wasn't updated to account for some changes in the object structure. After calling p <- plotly_build(p), we observe that there is no p$data, but there is p$x$data. So, changing the lapply call to the following:

p$x$data <- lapply(p$x$data, FUN = function(x){
 x$marker = list(opacity = 0)
 return(x)
})

makes everything work as intended:

enter image description here

Upvotes: 2

Related Questions