R在图中标记分位数

时间:2016-07-12 08:32:33

标签: r plot quantile

我想在此图中指出多个分位数,如this link

x <- rnorm(1000,0,1)

me <- mean(x)
s <- sd(x)

y <- 1/sqrt(2*pi)/s*exp(-(x-me)^2/2/s/s)
sx <- sort(x)
n <- seq(1, length(sx),1)
Px <- (3*n-1)/(3*length(sx)+1)

plot(sx,Px)
lines(sx, pnorm(sx, mean=me, sd = s), lwd=3, col="red")

您会推荐哪种功能?到目前为止我计算了分数。

qq<-quantile(x,c(0.5,0.95))

2 个答案:

答案 0 :(得分:1)

您可以尝试:

# data
set.seed(1221)
x <- rnorm(1000,0,1)
# [your code to plot the graph]
# Quantiles
P  <- c(0.05, .25, 0.5, 0.75, 0.95) # The quantiles you want to calculate
qq <- quantile(x, P)
df <- cbind(P, qq)
# the segments
apply(df, 1, function(x) segments(x0 = x[2], x1 = x[2], y0 = -10, y1 = x[1], lty = 2, col = 2))
apply(df, 1, function(x, y) segments(x0 = y-10, x1 = x[2], y0 = x[1], y1 = x[1], lty = 2, col = 2), min(x))

修改

# add the text, not that elegant, but it works:
sapply(1:length(names(qq)), function(x) text(df[x,2], -0.08, bquote(Q[ ~ .(names(qq)[x])]), xpd = TRUE))

enter image description here

答案 1 :(得分:0)

最粗暴的,你可以:

lines(x=c(qq[1], qq[1]), y=c(-2, 0.5), col="darkgreen", lwd=2, lty="dotted")
lines(x=c(-5, 0), c(0.5, 0.5), col="darkgreen", lwd=2, lty="dotted")

使用axis添加自定义标签。