2012-02-18 41 views
5

我想在XY圖中添加99%和95%的置信區間。xyplot帶有置信區間(盒子和wisker圖)添加在r

以下是數據:

X <- 1:20 
Y <- c(0.5, 1.4, 2.3, 3.4, 4.5, 
     3.3, 3.0, 2.1, 1.5, 0, 
     0, 3.4, 4.5, 6.7, 5.3, 2.8, 
     0.5, 3.4, 3.5, 3.7) 
mydata <- data.frame (X, Y) 

我想確定Y的最大值和相應的X值是在箱須圖中間的位置。每當Y的值下降1點(左或右)時,99%置信區間(將在框內),並且每當Y下降到2(左和右)時,x中的對應位置將被表示靠晶須。

所希望的描繪:

enter image description here

解釋。 enter image description here

對應的x值到最大(Y)= 6.7 對應的x值到框左= 6.7 - 1,框右= 6.7 - 1 對應的x值,以晶須左= 6.7 - 2,晶須右= 6.7 - 2

回答

1

以下情節應該讓你開始。它使用矩形而不是bwplot,也不會插值。

創建數據:

library(ggplot2) 

dat <- data.frame(
    x = 1:20, 
    y = c(0.5, 1.4, 2.3, 3.4, 4.5, 3.3, 3.0, 2.1, 1.5, 0, 0, 3.4, 4.5, 6.7, 5.3, 2.8, 0.5, 3.4, 3.5, 3.7) 
) 

編寫返回的5個希望點功能:

getRange <- function(x, a=1, b=2){ 
    maxy <- max(x) 
    xMax <- which.max(x) 
    x2 <- max(which(x[1:xMax] <= (maxy-a))) 
    x1 <- max(which(x[1:x2] <= (maxy-b))) 
    x3 <- xMax + min(which(x[-(1:xMax)] < (maxy+a))) 
    x4 <- x3 + min(which(x[-(1:x3)] < (maxy+b))) 
    data.frame(x1=x1, x2=x2, max=xMax, x3=x3, x4=x4) 
} 

獲取的範圍值和情節:

rr <- getRange(dat$y, 1, 3) 

ggplot(dat, aes(x, y)) + geom_line() + geom_point() + 
    geom_rect(data=rr, aes(xmin=x2, xmax=x3, NULL, NULL), 
       ymin=-Inf, ymax=Inf, fill="blue", alpha=0.25) + 
    geom_rect(data=rr, aes(xmin=x1, xmax=x4, NULL, NULL), 
       ymin=-Inf, ymax=Inf, fill="blue", alpha=0.25) 

enter image description here

+0

我們可以畫箱子嗎?多邊形和wshiker作爲基線r圖形中的線段_一旦我們已經知道數據點? – SHRram 2012-02-18 21:48:51