Skip to content

Mistake in documentation of ggplotly box plots #58

Open
@meldarionqeusse

Description

@meldarionqeusse

In this explanation for how to implement geom_boxplot with plotly (https://plotly.com/ggplot2/box-plots/#outliers) there is a mistake in the function used.

Currently the code example reads like this:

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
fig <- plotly_build(p)

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

fig

it should be

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
fig <- plotly_build(p)

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

fig

Note the difference fig$x$data instead of fig$data otherwise this won't remove the outliers.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions