wen*_*wen 4 plot r graph area ggplot2
我正在绘制标准正态分布。
curve(dnorm(x), from=-4, to=4,
main = "The Standard Normal Distibution",
ylab = "Probability Density",
xlab = "X")
Run Code Online (Sandbox Code Playgroud)
出于教学原因,我想将低于我选择的某个分位数的区域涂上阴影。我怎样才能做到这一点?
如果你想使用curve
和基图,那么你可以自己写一个小函数polygon
:
colorArea <- function(from, to, density, ..., col="blue", dens=NULL){
y_seq <- seq(from, to, length.out=500)
d <- c(0, density(y_seq, ...), 0)
polygon(c(from, y_seq, to), d, col=col, density=dens)
}
Run Code Online (Sandbox Code Playgroud)
一个小例子如下:
curve(dnorm(x), from=-4, to=4,
main = "The Standard Normal Distibution",
ylab = "Probability Density",
xlab = "X")
colorArea(from=-4, to=qnorm(0.025), dnorm)
colorArea(from=qnorm(0.975), to=4, dnorm, mean=0, sd=1, col=2, dens=20)
Run Code Online (Sandbox Code Playgroud)