繁体   English   中英

将误差线添加到 plotly 框 plot 内的点

[英]Add error bars to points within a plotly box plot

我正在为这些数据创建一个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)

给了我上面相同的 plot。

但是,如果我只使用 plot 点并添加它们的错误:

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)

我确实得到了带有垂直误差线的点:

所以我的问题是如何让盒子+点+误差线工作? 而且,如果解决方案还可以将抖动点与其误差线结合起来,那就更好了。

您可以在绘制点和误差线后添加框 plot。

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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