標準正規分布のテイルを塗りつぶす。
library(ggplot2)
library(gridExtra)
limitRange <- function(fun, min, max) {
function(x) {
y <- fun(x)
y[x < min | x > max] <- NA
return(y)
}
}
# p(x>2)
p <- ggplot(data.frame(x=c(-4, 4)), aes(x=x))
p + stat_function(fun = dnorm) +
stat_function(fun = limitRange(dnorm, 2, 4),
geom="area", fill="blue", alpha=0.2)
# 95%
p <- ggplot(data.frame(x=c(-4, 4)), aes(x=x))
p + stat_function(fun = dnorm) +
stat_function(fun = limitRange(dnorm, -1.96, 1.96),
geom="area", fill="blue", alpha=0.2)