gpt4 book ai didi

r - 将误差线添加到 plotly box plot 中的点

转载 作者:行者123 更新时间:2023-12-05 05:03:00 27 4
gpt4 key购买 nike

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

但是,如果我只绘制点并使用以下方法添加它们的错误:

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)

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

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

最佳答案

您可以在绘制点和误差线后添加箱线图。

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)

关于r - 将误差线添加到 plotly box plot 中的点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61947629/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com