如何将文本添加到r中的绘图框图中

时间:2017-01-11 18:47:53

标签: r plotly boxplot outliers

我想标记出现在我的图表上的异常值。这可能与情节有关吗?

我的图表代码在这里:

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 个答案:

答案 0 :(得分:2)

目前尚不清楚您尝试了什么以及哪些不起作用,但识别异常值的一种方法是使用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