Добавление полос погрешностей к точкам на прямоугольной диаграмме

Я создаю R plotly boxplot для этих данных:

set.seed(1)
df <- data.frame(value = rnorm(100),
                 value.error. = runif(100,0.01,0.1), 
                 treatment = rep(LETTERS[1:10], 10), 
                 replicate = rep(1:10, 10), stringsAsFactors = F)
df$treatment <- factor(df$treatment)

Где в каждом поле я добавляю реплики в виде точек:

library(dplyr)
plotly::plot_ly(x = df$treatment, split = df$treatment, y = df$value, 
                type = "box", showlegend = F, color = df$treatment,
                boxpoints = F, fillcolor = 'white') %>%
  plotly::add_trace(x = df$treatment, y = df$value, type = 'scatter', mode = "markers", 
                    marker = list(size = 8), showlegend = F, color = df$treatment)

Который дает:

Теперь я хотел бы добавить вертикальные полосы погрешностей к каждой точке (согласно df$value.error).

Этот:

plotly::plot_ly(x = df$treatment, split = df$treatment, y = df$value,
                type = "box", showlegend = F, color = df$treatment, 
                boxpoints = F, fillcolor = 'white') %>%
  plotly::add_trace(x = df$treatment, y = df$value, type = 'scatter', mode = "markers", 
                    marker = list(size = 8), showlegend = F, color = df$treatment) %>%
  plotly::add_trace(error_y = list(array = df$sd), showlegend = F)

Дает мне тот же сюжет, что и выше.

Однако, если я только нанесу точки и добавлю их ошибки, используя:

plotly::plot_ly(x = df$treatment, y = df$value, 
                type = 'scatter', mode = "markers", 
                marker = list(size = 8), showlegend = F, color = df$treatment) %>%
  plotly::add_trace(error_y =list(array = df$sd), showlegend = F)

Я получаю очки с их вертикальными полосами погрешностей:

Итак, мой вопрос: как заставить работать поле + точки + шкала ошибок? И, если решение также может сочетать дрожание точек с их планками погрешностей, это будет еще лучше.


person dan    schedule 22.05.2020    source источник


Ответы (1)


Вы можете добавить коробчатую диаграмму после нанесения точек и полос погрешностей.

library(plotly)

plot_ly(data = df,
        x = ~treatment, y = ~value, 
        type = 'scatter', mode = "markers", 
        marker = list(size = 8), showlegend = F, color = df$treatment) %>%
  add_trace(error_y =list(array = ~value.error.), showlegend = F) %>% 
  add_boxplot(x = ~treatment, split = ~treatment, y = ~value, 
              showlegend = F, color = ~treatment,
              boxpoints = F, fillcolor = 'white')

Данные:


set.seed(1)
df <- data.frame(value = rnorm(100), 
                 value.error. = runif(100,0.01,0.1), 
                 treatment = rep(LETTERS[1:10], 10), 
                 replicate = rep(1:10, 10), 
                 stringsAsFactors = F)
df$treatment <- factor(df$treatment)
person M--    schedule 22.05.2020