gpt4 book ai didi

r - 如何在 R 中使用曲线()对图形进行着色

转载 作者:行者123 更新时间:2023-12-01 11:35:03 24 4
gpt4 key购买 nike

我正在绘制标准正态分布。

curve(dnorm(x), from=-4, to=4, 
main = "The Standard Normal Distibution",
ylab = "Probability Density",
xlab = "X")

出于教学原因,我想将低于我选择的某个分位数的区域涂上阴影。我怎样才能做到这一点?

最佳答案

如果您想使用 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)
}

一个小例子如下:
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)

enter image description here

关于r - 如何在 R 中使用曲线()对图形进行着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27898931/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com