2015-09-03 32 views
0

我想要創建一個ecdf plot with two lines,我想將errorbars添加到其中的一個。geg_errorbar與ecdf在ggplot

我使用此代碼

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

它打印錯誤欄,但情節已完全中斷。

+1

你想達到什麼不清楚。你想在第一個數據集上有兩個曲線嗎?或者只是名稱爲'x'的曲線? –

+0

@ColonelBeauvel ust曲線名稱x –

+0

我會先計算ecdf,然後將它合併到data.frame,並且只用它繪製'geom_errorbar'。 –

回答

0

有一些類似的問題在這裏: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)