将误差条添加到 plotly 框内的点上。

问题描述 投票:2回答:1

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

我得到的点和它们的垂直误差条。

所以我的问题是如何让盒子+点+误差条来工作? 如果解决方案能把点和误差条的抖动结合起来,那就更好了。

r plotly boxplot r-plotly errorbar
1个回答
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)
© www.soinside.com 2019 - 2024. All rights reserved.