将误差线添加到分组的 R 绘图条形图

Adding error bars to a grouped R plotly bar chart

我有数据要使用带有 y 误差条的 R plotly 条形图绘制。数据由两组组成,每组具有三种类型的测量值:

set.seed(1)
df <- data.frame(group = c(rep("A",3),rep("B",3)),
                 type = rep(letters[1:3],2),
                 mean.proportion = runif(6,0,1),
                 se.proportion = runif(6,0.01),
                 stringsAsFactors = F)
df$group <- factor(df$group)
df$type <- factor(df$type)

如果我只绘制没有错误条的条使用:

plotly::plot_ly(x=df$type,y=df$mean.proportion,type='bar',color=df$group,showlegend=T) %>%
  plotly::layout(xaxis=list(title=NA),yaxis=list(title="Proportion"),barmode='group')

结果很好:

但是,当我尝试使用以下方式添加 y 误差线时:

plotly::plot_ly(x=df$type,y=df$mean.proportion,type='bar',color=df$group,showlegend=T) %>%
  plotly::layout(xaxis=list(title=NA),yaxis=list(title="Proportion"),barmode='group') %>%
  plotly::add_trace(error_y=list(array=df$se.proportion))

搞砸了——胸罩加倍了:

有什么想法吗?

尝试在第一个语句中绘制它们:

plotly::plot_ly(x=df$type,y=df$mean.proportion,type='bar',color=df$group,showlegend=T, error_y=list(array=df$se.proportion)) %>%
  plotly::layout(xaxis=list(title=NA),yaxis=list(title="Proportion"),barmode='group')

输出: