Skip to content

Boxplot outliers are shown in black using ggplotly #1114

Open
@dfrail24

Description

@dfrail24

If I create a boxplot in ggplot2 and convert it using ggplotly command, the outliers are outlined in black.
Here is a simple example:

library(ggplot2)
library(plotly)

p <- ggplot(mpg, aes(class, hwy))
g <- p + geom_boxplot(aes(colour = "red"))
ggplotly(g)

ggplot would show this chart:
image

whereas plotly would show this chart:
image

Is this something that can be fixed?

Activity

jonocarroll

jonocarroll commented on Nov 17, 2017

@jonocarroll

This persists even when the outliers should be discarded, in the examples also

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)
})

p

screen shot 2017-11-17 at 6 54 06 pm

petehilljnr

petehilljnr commented on Apr 30, 2018

@petehilljnr

I managed to set the opacity property of the outliers using the code below. This seems to work for the faceted charts I have tried so far also.

library(ggplot2)
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)

for(i in 1:length(p$x$data)) {
  p$x$data[[i]]$marker$opacity = 0
}

p

image

jonocarroll

jonocarroll commented on May 1, 2018

@jonocarroll

The replacement lapply code is then

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

(note p$x$data rather than p$data). I'm happy to PR this to the documentation if someone can point to the source.

dmattek

dmattek commented on Jul 10, 2018

@dmattek

The problem is that when you also have geom_jitter in the plot (in addition to geom_boxplot), the lapply part will remove all the points. Is there a way to selectively remove outliers that belong to geom_boxplot only?

ningjingzhiyuan507

ningjingzhiyuan507 commented on Jul 19, 2018

@ningjingzhiyuan507

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

modify marker$line$color

brshallo

brshallo commented on Feb 25, 2019

@brshallo

The problem is that when you also have geom_jitter in the plot (in addition to geom_boxplot), the lapply part will remove all the points. Is there a way to selectively remove outliers that belong to geom_boxplot only?

You can use the code above and just index to the layer you want to remove, e.g. say the boxplot outliers are on the first layer.

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

kojisposts commented on Jul 12, 2019

@kojisposts

Hi! Just wanted to bring this issue to your attention again, as none of the workarounds mentioned above seem to be working (and aren't working in the documentation either)!
(There's also an interesting phenomenon where, for coloured barplots, the most extreme outliers are coloured with black outlines, but closer to the barplot, they're black with coloured outlines, i.e. the reverse.)

cpsievert

cpsievert commented on Jul 12, 2019

@cpsievert
Collaborator

There's a WIP here #1514 that fixes this issue, feel free to test it out and let me know if you run into problems.

jcoronel25

jcoronel25 commented on Dec 12, 2019

@jcoronel25

I didn't see the solution being mentioned #1514 on the last release. I was to get the visual I wanted by altering the lapply function to filter only layer that are type == "box"

p$x$data <- lapply(p$x$data, FUN = function(x){

  if (x$type == "box") {
    x$marker = list(opacity = 0)
  }
  return(x)
})
isaaczhao23

isaaczhao23 commented on Feb 26, 2020

@isaaczhao23

This will do the trick for the original question coloring outliers! Plotly differentiates outliers from extreme outliers. We go under the hood and override all outlier colors manually.

library(ggplot2)
library(plotly)

p <- ggplot(mpg, aes(class, hwy)) + geom_boxplot(color="red")
output = ggplotly(p)

# overrides black outline of outliers
output$x$data[[1]]$marker$line$color = "red"
# overrides black extreme outlier color
output$x$data[[1]]$marker$outliercolor = "red"
# overrides black not as extreme outlier color
output$x$data[[1]]$marker$color = "red"

output

image

10 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @cpsievert@lucazav@petehilljnr@jonocarroll@jcoronel25

        Issue actions

          Boxplot outliers are shown in black using ggplotly · Issue #1114 · plotly/plotly.R