標準正規分布を描くコード。領域を塗りつぶします。
library(ggplot2)
limitRange <- function(fun, min, max) {
function(x) {
y <- fun(x)
y[x < min | x > max] <- NA
return(y)
}
}
ggplot(data.frame(x=c(-4, 4)), aes(x=x)) + stat_function(fun = dnorm) +
stat_function(fun = limitRange(dnorm, -1, 1),
geom="area", fill="blue", alpha=0.2)+
stat_function(fun = limitRange(dnorm, -2, 2),
geom="area", fill="blue", alpha=0.2)+
stat_function(fun = limitRange(dnorm, -3, 3),
geom="area", fill="blue", alpha=0.2)