简体   繁体   中英

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?

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.

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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