在 Plotly 的箱线图中为点添加误差条

3

我正在创建一个 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)

我得到了它们的垂直误差条的点:

所以我的问题是如何让框架、点和误差条一起使用?而且,如果解决方案还能将点的抖动与其误差线结合起来,那就更好了。

1个回答

2
您可以在绘制点和误差棒之后添加箱线图。
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')

Data:


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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接