geom_errorbar 在 ggplot 中使用 ecdf

geom_errorbar with ecdf in ggplot

我想创建一个包含两条线的 ecdf 图,我想向其中一条线添加误差线。

我正在使用这个代码

x <- c(16,16,16,16,34,35,38,42,45,1,12)
xError <- c(0,1,1,1,3,3,3,4,5,1,1)
y <- c(16,1,12)

length(x)
length(xError)
length(y)

df <-  rbind(data.frame(value = x,name='x'),
             data.frame(value = y,name='y'))

ggplot(df, aes(x=value,color=name,linetype=name))+ stat_ecdf()+ geom_errorbar(aes(ymax = x + xError, ymin=x - xError))

错误栏应该添加到 x 值,但它给我这个错误:

Error: Aesthetics must either be length one, or the same length as the dataProblems: x + xError, x - xError

我不明白 - 结果是相同的长度。

编辑

我改成了问题,所以它变得更容易了——我认为真正的问题与 ECDF 图和误差线有关。以此代码为例:

x <- c(16,16,16,16,34,35,38,42,45,1,12)
xError <- c(0,1,1,1,3,3,3,4,5,1,1)
y <- c(16,1,12)

df <- data.frame(value = x)

ggplot(df, aes(x=value))+ stat_ecdf()+ geom_errorbar(aes(ymax = x + xError, ymin=x - xError))

它打印了误差线,但情节完全被打破了。

这里有一些类似的问题:confidence interval for ecdf

也许这就是您想要归档的内容。

编辑:

我认为这就是您要尝试获得的东西:

dat2 <- data.frame(variable = x)
dat2 <- transform(dat2, lower = x - xError, upper = x + xError)

l <- ecdf(dat2$lower)
u <- ecdf(dat2$upper)
v <- ecdf(dat2$variable)

dat2$lower1 <- l(dat2$variable)
dat2$upper1 <- u(dat2$variable)
dat2$variable1 <- v(dat2$variable)

ggplot(dat2,aes(x = variable)) + 
  geom_step(aes(y = variable1)) + 
  geom_ribbon(aes(ymin = upper1,ymax = lower1),alpha = 0.2)