如何在R中将文本添加到绘图箱图 [英] How to add text to a plotly boxplot in r

查看:99
本文介绍了如何在R中将文本添加到绘图箱图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想标记图表上显示的异常值.用plotly可以做到吗?

I would like to mark the outlier that appears on my chart writing where it is. Is this possible with 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(),然后可以使用该信息添加注释

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

这篇关于如何在R中将文本添加到绘图箱图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆