我想在 XYplot 中添加 99% 和 95% 的置信区间。
以下是数据:
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 中的相应位置由 mustache 。
想要的剧情:
解释。
对应于 max(Y) = 6.7 的 x 值
对应于左侧框的 x 值 = 6.7 - 1,右侧框 = 6.7 - 1
对应于左边晶须的 x 值 = 6.7 - 2,右边晶须 = 6.7 - 2
最佳答案
下面的情节应该让你开始。它使用矩形而不是 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)
关于r - 在 r 中添加了置信区间(箱线图和 wisker 图)的 xyplot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9342529/