0

I want to create an ecdf plot with two lines and I would like to add errorbars to one of them.

I am using this code

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

The error bar should be added to the x values, but it gives my this error:

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

I don't get it - the result is of the same length.

EDIT

I changed to problem, so it gets easier - I thin the real problem is related to ECDF plots and error bars. Take this code as an example:

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

It prints the error bars, but the plot is completely broken.

s.mustav
  • 3
  • 2

1 Answers1

0

there is some similar question here: confidence interval for ecdf

Maybe thats the thing You'll like to archive.

EDIT:

I think this is the thing You'll try to get:

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)
thardes2
  • 1,162
  • 12
  • 28