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

时间:2018-11-09 22:21:45

标签: r bar-chart plotly errorbar

我想使用带有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')

结果很好:

enter image description here

但是,当我尝试使用以下方式添加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))

一团糟-胸罩加倍: enter image description here

有什么主意吗?

1 个答案:

答案 0 :(得分:1)

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

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')

输出:

enter image description here