从箱线图和Plotly中删除异常值

9
我正在尝试在R中创建一个Plotly箱线图,但不想显示异常值。我在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

问题在于在他们的网页和我的控制台中,仍然显示异常值。 在这里输入图片描述 这是某种 bug 吗?

随着时间的推移,错误可能会被修复。对我来说,geom_boxplot(outlier.shape = NA) 生成一个没有异常值的箱线图。 - Ufos
1个回答

2
似乎是一个打字错误。也许这个例子没有更新以适应对象结构的一些变化。在调用 p <- plotly_build(p) 后,我们观察到没有 p$data,但有 p$x$data。因此,将 lapply 调用更改为以下内容:
p$x$data <- lapply(p$x$data, FUN = function(x){
 x$marker = list(opacity = 0)
 return(x)
})

使一切按预期运行:

enter image description here


1
这不仅使得离群值透明,而且所有点都变成了透明。使用geom_jitter层后,没有任何点显示出来。我只能找到关于离群值设置的p$x$data$marker$outliercolor,但是它并没有按照预期工作。如果我想看到其他点,你有什么想法吗? - Sean Lin
@SeanLin,使用jitter时忽略异常值的解决方案有吗? - user5249203

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接