Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding boxplot outlier tooltips in ggplotly

Tags:

r

ggplot2

plotly

I know how to hide outliers with ggplotly and geom_boxplot, as per Removing outliers from boxplot and plotly :

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, outlier.size = NA, outlier.colour = NA) + 
  ggtitle("Ignore outliers in ggplot2")

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

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

fig

But while the visual outlier points are gone, the tooltips remain when you hover over where the points would be, e.g.:

example

Is there any way to get rid of those specifically while keeping the other tooltips?

like image 306
mdhall272 Avatar asked Jun 27 '26 14:06

mdhall272


1 Answers

This worked for me:

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, outlier.size = NA, outlier.colour = NA) + 
  ggtitle("Ignore outliers in ggplot2")

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

fig$x$data <- lapply(fig$x$data, FUN = function(x){
  x$marker = list(opacity = 0)
###### added line to remove hoverinfo:
  x$hoverinfo = "none"
###### end of addition
  return(x)
})

fig
like image 194
Leon Samson Avatar answered Jun 29 '26 04:06

Leon Samson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!