问题
I would like to mark the outlier that appears on my chart writing where it is. Is this possible with plotly?
The code of my graph is here:
library(plotly)
set.seed(1234)
plot_ly(y = rnorm(50), type = 'box') %>%
add_trace(y = rnorm(50, 1)) %>%
layout(title = 'Box Plot',
xaxis = list(title = "cond", showgrid = F),
yaxis = list(title = "rating"))
回答1:
It's not clear what you tried and what's not working, but one way to identify outliers is to use boxplot.stats()
and then you can use that information to add annotations.
library(plotly)
set.seed(1234)
d <- rnorm(50)
d2 <- rnorm(50, 1)
plot_ly(y = d, type = 'box') %>%
add_trace(y = d2) %>%
layout(title = 'Box Plot',
xaxis = list(title = "cond", showgrid = F),
yaxis = list(title = "rating"),
annotations = list(
x = -0.01,
# use boxplot.stats() to get the outlier's y coordinate
y = boxplot.stats(d)$out,
text = "Outlier",
showarrow = FALSE,
xanchor = "right"
)
)
来源:https://stackoverflow.com/questions/41598448/how-to-add-text-to-a-plotly-boxplot-in-r