-
Notifications
You must be signed in to change notification settings - Fork 627
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Boxplot outliers are shown in black using ggplotly #1114
Comments
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 |
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 |
The replacement p$x$data <- lapply(p$x$data, FUN = function(x){
x$marker = list(opacity = 0)
return(x)
}) (note |
The problem is that when you also have |
modify marker$line$color |
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.
|
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 a WIP here #1514 that fixes this issue, feel free to test it out and let me know if you run into problems. |
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"
|
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.
|
While I have not solved the issue above. I would like to add a different more complete solution to the provided by @isaaczhao23 in cases where there are boxplots with different colors. df <- diamonds[sample(1:nrow(diamonds), size = 1000),]
#df <- subset(df,df$cut=="Fair")
p <- ggplot(df, aes(cut, price, color = cut)) +
geom_boxplot()
# 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$outliercolor = x$line$color # When creating plot p with ggplot if you specify fill = cut use x$fill$color instead of $line$color
x$marker$color = x$line$color # When creating plot p with ggplot if you specify fill = cut use x$fill$color instead $line$color
x$marker$line = x$line$color # When creating plot p with ggplot if you specify fill = cut use x$fill$color instead $line$color
return(x)
})
fig Will produce this: |
I'm still having the issue of outliers shown after apply |
In the meantime I solved the issue of hiding outliers using the following code:
It works also with facets. |
Thank you so much for this solution, @brshallo ! This fixed my problem after hours of looking for a fix! |
As a novice, I'm having a hard time understanding what this code means. Would you or anyone else mind using this in a reproducible example? Thank you. |
I used the information in this thread to create the following function, combining techniques described earlier for the specific use case that you mention. It might be helpful for others:
it only removes markers if
Figure with outliers:
Figure with outliers removed:
|
If I create a boxplot in ggplot2 and convert it using ggplotly command, the outliers are outlined in black.
Here is a simple example:
ggplot would show this chart:
whereas plotly would show this chart:
Is this something that can be fixed?
The text was updated successfully, but these errors were encountered: