R 标记图中的分位数

R mark quantiles in a plot

我想指出此图中的多个分位数,如 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))

在最粗略的情况下,您可以:

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 添加自定义标签。

你可以试试:

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