Xyplot with confidence interval (box and wisker field) added to r

I want to add a confidence interval of 99% and 95% in XYplot.

The following is the data:

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)

I want to determine the maximum value of Y, and the corresponding value of X is the position of the median field in the field and whisker. Whenever the Y value decreases by 1 point (left or right), this is a confidence interval of 99% (will be inside the field), and whenever Y decreases to 2 (both left and right), the corresponding positions will be shown in x mustache.

Desired plot:

enter image description here

Explanation. enter image description here

the corresponding value x max (Y) = 6.7 the corresponding x value in the field on the left = 6.7 - 1, the field on the right = 6.7 - 1 the corresponding x value for the whisker on the left = 6.7 - 2, the insert on the right = 6.7 - 2

+5
source share
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

+1

All Articles