简体   繁体   English

如何使用百分比在条形图上添加误差线

[英]How to Add Error Bars on Barplot With Percentages

New to R, apologies in advance. R的新手,提前道歉。 Created the attached barplot using ggplot2 with percentages使用带有百分比的 ggplot2 创建附加的条形图

图像1

I am trying to add error bars to it and I can't seem to find a way to do so.我正在尝试向其中添加错误栏,但似乎找不到这样做的方法。 I'm not sure if my data will work as is or if it needs to be altered, data table is below as well我不确定我的数据是否会按原样工作或是否需要更改,数据表也在下面

图像2

I can't seem to figure out how to keep percentages and add error bars.我似乎无法弄清楚如何保持百分比和添加误差线。 I have tried geom_errorbars in various ways but this will not work.geom_errorbars各种方式尝试过geom_errorbars ,但这不起作用。 I tried the following and the error bars are missing and averages are listed, no percentages:我尝试了以下操作,但缺少误差条并列出平均值,没有百分比:

SI_avgs <- Shift_Injuries %>%
  group_by(Shift) %>% 
  summarize(Avg = mean(counnt),
        SD = sd(counnt),
        N = n(counnt)) %>% 
  ungroup() %>% 
  mutate(L_CI = Avg - 1.96 * sqrt(Avg * (1 - Avg) / N)) %>% 
  mutate(U_CI = Avg + 1.96 * sqrt(Avg * (1 - Avg) / N))
SI_avgs
ggplot(SI_avgs, aes(Shift, Avg)) +
  geom_col(fill = "royalblue1") + 
  geom_errorbar(aes(ymin = L_CI, ymax = U_CI), width = .25)

Any suggestions would be appreciated.任何建议,将不胜感激。

use geom_bar instead of geom_col and then use geom_errorbar使用geom_bar而不是geom_col然后使用geom_errorbar

ggplot(data) +
    geom_bar( aes(x=Shift, y=Avg), stat="identity", fill="royalblue1") +
    geom_errorbar( aes(x=Shift, ymin=Avg-sd, ymax=Avg+sd), width=0.25)

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

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