2015-01-12 1 views
2

표준 정규 분포를 도출하고 있습니다.curve()를 사용하여 그래프를 음영 처리하는 방법 R

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

나는 교육의 이유로, 내가 선택한 특정 분위 아래의 영역을 음영 처리하고 싶습니다. 어떻게해야합니까? 당신이 curve 및 기본 플롯을 사용하려면

+1

HTTP : //stackoverflow.com/questions/12429333/how-to-shade-a-region-under-a-curve-using-ggplot2 –

답변

4

, 당신은 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

관련 문제