r - 如何将文本添加到 r 中的绘图箱线图中

标签 r plotly boxplot outliers

我想标记图表上出现的异常值,并写出它的位置。这可以用 plotly 来实现吗?

我的图表的代码在这里:

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"))

最佳答案

尚不清楚您尝试了什么以及什么不起作用,但识别异常值的一种方法是使用 boxplot.stats(),然后您可以使用该信息添加注释。

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"
         )
  )

enter image description here

关于r - 如何将文本添加到 r 中的绘图箱线图中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41598448/

相关文章:

r - 如何在新行中分隔 R 中的逗号分隔值?

r - 合并数据集而不丢失数据

python - 在 plotly 3D 轴和刻度标签中使用 LaTeX

r - 向水平箱线图添加点

python - 如何在箱线图中显示 Pandas DataFrame 的最后一行

r - 如何从 Google Analytics 获取 R 中的大型数据集?

r - 你如何为 data.frame 子类扩展 rbind?

Python - 当行索引和列都是类别时数据帧值的散点图

python - Plotly:如何根据条件为两条线之间的填充着色?

r - 在geom_boxplot中包含用于填充美学的缺失因子水平的空间