如何手动将误差线(95%CI)添加到条形图

时间:2019-05-31 16:53:05

标签: r ggplot2

我用ggplot2绘制了一个累积条形图:

corecaptured
 ^

它看起来像这样: barplot without error bar

我想为每列添加一个错误栏。

因此,首先,我手动计算了每个95%CI:

a <- ggplot(datasilice2) +
  aes(x = Duree_exposition_4gpes, fill = factor(Antinucléaires_sup_ou_egal_200)) +
  geom_bar(position = "fill", show.legend = F, colour = "NA", alpha = 0.4) +
  scale_fill_manual(values=c(NA, "red")) +
  scale_y_continuous(name = "Yes", limits=c(0,0.5),oob = rescale_none, breaks = seq(0,0.5,0.05), labels = percent_format(suffix = "%", accuracy = 1))

然后,我使用geom_errorbar函数:

binom.test(2,45)$conf.int*100 
binom.test(3,10)$conf.int*100 
binom.test(5,18)$conf.int*100 
binom.test(12,27)$conf.int*100 

它确实绘制了一个误差线,但是每个误差线都是相同的: same error bar

现在我被困住了:我找不到手动设置每个错误栏的解决方案。我曾想过像这样的事情,但它行不通:

a + geom_errorbar(ymin=0.2, ymax=0.4,width = 0.2, size=1)

这是我得到的错误:

a + geom_errorbar(ymin=c(0.2,0.22,0.23,0.30), ymax=c(0.4,0.43,0.35,0.50))
#random number, I will put CI here

关于如何获得这样的东西的任何想法? (done with paint) 谢谢!

编辑: 这是我的数据库(datasilice2)的前10行:

Aesthetics must be either length 1 or the same as the data (100): ymin, ymax

根据建议,我创建了一个新的数据框,并尝试将其输入到geom_errorbar中:

> dput(head(datasilice2, 10))
structure(list(Duree_exposition_4gpes = c(3, 3, 0, 1, 0, 1, 2, 
2, 1, 3), Antinucléaires_sup_ou_egal_200 = c(1, 0, 0, 1, 1, 1, 
0, 0, 1, 1)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", 
"data.frame"))

但这是一个新错误:

a <- ggplot(datasilice2) +
  aes(x = Duree_exposition_4gpes, fill = factor(Antinucléaires_sup_ou_egal_200)) +
  geom_bar(position = "fill", show.legend = F, colour = "NA", alpha = 0.4) +
  scale_fill_manual(values=c(NA, "red")) +
  scale_y_continuous(name = "Yes", limits=c(0,0.5),oob = rescale_none, breaks = seq(0,0.5,0.05), labels = percent_format(suffix = "%", accuracy = 1))

newdf <- data.frame(xcol=c(0,1,2,3),ymincol=c(0.2,0.3,0.4,0.5),ymaxcol=c(0.25,0.30,0.45,0.5))
a + geom_errorbar(data=newdf,aes(x=xcol,ymin=ymincol,ymax=ymaxcol))

2 个答案:

答案 0 :(得分:1)

您需要将手动计算的数据放入数据框中,然后将其输入public void ConfigureServices(IServiceCollection services) { // other configuration here services.AddMvc() .AddJsonOptions(options => options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore); } 中。我认为geom_errorbar不接受向量。因此,您的新数据框,例如ggplot,应具有x值,y min值和ymax值的列。然后,应该可以执行以下操作:newdf(列名称必须与您决定在数据框中命名的名称相匹配)。

答案 1 :(得分:0)

以下代码将以百分比比例添加误差线。

在两个中间条中,误差条一个上升至100%,另一个误差条开始于0%,因为每个组的所有数据点分别为1或0。实际数据可能会改变。

library(ggplot2)
library(scales)
library(dplyr)

datasilice2 %>%
  group_by(Duree_exposition_4gpes) %>%
  summarise(N = n(),
            Sum = sum(Antinucléaires_sup_ou_egal_200),
            LimInf = binom.test(Sum, N)$conf.int[1],
            LimSup = binom.test(Sum, N)$conf.int[2],
            Sum = Sum/N) %>%
  ggplot(aes(x = Duree_exposition_4gpes, y = Sum,
             fill = factor(Sum))) +
  geom_bar(stat = "identity",
           position = position_dodge(), show.legend = FALSE, 
           colour = NA, alpha = 0.4) +
  scale_fill_manual(values = rep("red", 4)) +
  geom_errorbar(aes(ymin = LimInf, ymax = LimSup,
                    width = 0.2)) +
  scale_y_continuous(name = "Yes",
                     limits=c(0, 1),
                     oob = rescale_none, 
                     breaks = seq(0, 1, 0.05), 
                     labels = percent_format(suffix = "%", accuracy = 1))

enter image description here