简体   繁体   English

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

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

I would like to mark the outlier that appears on my chart writing where it is. 我想标记图表上显示的异常值。 Is this possible with plotly? 用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"))

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