繁体   English   中英

如何在R中将文本添加到绘图箱图

[英]How to add text to a plotly boxplot in r

我想标记图表上显示的异常值。 用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"
         )
  )

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM