2017-04-19 93 views
4

我想創建一個plotly箱線圖中的R,這並不表明了異常,我發現在plotly官方頁面此鏈接: https://plot.ly/ggplot2/box-plots/#outliers從箱線圖中刪除離羣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) + 
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 

的問題是,在他們的網頁和我的控制檯中,異常值仍在顯示。 enter image description here 這是一種錯誤嗎?

+0

隨着時間的推移,錯誤可能會得到修復。 'geom_boxplot(outlier.shape = NA)'給我產生了一個沒有異常值的盒子圖。 – Ufos

回答

1

看起來像一個錯字。也許這個例子沒有被更新來解釋對象結構的一些變化。在致電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

+0

這不僅僅使離羣值變得透明,而且實際上所有點都是透明的。通過geom_jitter圖層,不出現任何點。關於異常值設置,我只能找到'p $ x $ data $ marker $ outliercolor',但不能按預期工作。你有什麼想法,如果我想看其他點? –