问题
I am plotting jitter boxplots through plotly in R. Plotly boxplots allow analyzing interactively the quartiles and the values of outliers. (Examples here: https://plot.ly/r/box-plots/)
I would like to see the name of observations that are outliers, so I can analyze them later.
However, it seems that boxplots don't have the option of watching to which observation they belong to, in contrast to scatter boxplots, where one can see it through 'text' option.
Before implementing other approaches, however, I would like to confirm that there is no possibility to have this information plotted.
回答1:
I didn't find this option also. I tried to plot but I didn't succeed, so I Located the outlier with the function boxplot.stats and wrote over it.
Look this example:
set.seed(1234)
a<-rnorm(50)
a2 <- rnorm(50, 1)
plot_ly(y = a, type = 'box') %>%
add_trace(y = a2) %>%
layout(title = 'Box Plot',xaxis = list(title = "cond", showgrid = F), yaxis = list(title = "rating"),
annotations = list(
x = -0.01,
y = boxplot.stats(a)$out,
text = "Outlier",
showarrow = FALSE,
xanchor = "right"
))
回答2:
If you still want the outliers labeled by tooltips you can also identify them separately and pass the outliers dataset to add_marker()
, overwriting the boxplot outliers. Try something like this:
#Set seed
set.seed(9)
#Generate random dataset
x <- data.frame(values = rnorm(100,sd=2),labels = paste("point",as.character(1:100)))
#Get outliarsdata
vals <- boxplot(x[,"values"],plot = FALSE)
#Make outliars dataset
y <- x[x[,"values"] > vals$stats[5,1] | x[,"values"] < vals$stats[1,1],]
#Make plot
plot_ly(x,y = ~values,x = 1,type = "box") %>%
add_markers(data = y, text = y[,'labels'])
来源:https://stackoverflow.com/questions/40266253/show-observations-that-are-outliers-in-plot-ly