Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 927
Jorge Kageyama Avatar asked Apr 19 '17 14:04

Jorge Kageyama


1 Answers

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

like image 190
Artem Sokolov Avatar answered Oct 09 '22 21:10

Artem Sokolov