Created
May 17, 2016 15:50
-
-
Save sebastiansauer/7bb1e105960dfdbec639e836292772cf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# normal distribution with serveral shaded areas | |
library(ggplot2) | |
library(dplyr) | |
mean.1 <-0 | |
sd.1 <- 1 | |
zstart <- 2 | |
zend <- 3 | |
zcritical <- 1.65 | |
fom_gruen <- "#00998a" | |
x <- seq(from = mean.1 - 3*sd.1, to = mean.1 + 3*sd.1, by = .01) | |
# from 3sd left to 3sd right to the mean | |
MyDF <- data.frame(x = x, y = dnorm(x, mean = mean.1, sd = sd.1)) | |
shade_nv <- function(MyDF, zstart, zend, fill = "red", alpha = .5){ | |
geom_area(data = subset(MyDF, x >= mean.1 + zstart*sd.1 | |
& x < mean.1 + zend*sd.1), | |
aes(y=y), fill = fill, color = NA, alpha = alpha) | |
} | |
p1a <- ggplot(MyDF, aes(x = x, y = y)) + geom_line() + | |
shade_nv(MyDF = MyDF, zstart = -1, zend = 1, fill = fom_gruen, alpha = .3) + | |
shade_nv(MyDF = MyDF, zstart = 1, zend = 2, fill = fom_gruen, alpha = .5) + | |
shade_nv(MyDF = MyDF, zstart = -2, zend = -1, fill = fom_gruen, alpha = .5) + | |
shade_nv(MyDF = MyDF, zstart = 2, zend = 6, fill = fom_gruen, alpha = .7) + | |
shade_nv(MyDF = MyDF, zstart = -3, zend = -2, fill = fom_gruen, alpha = .7) + | |
scale_x_continuous(breaks = -3:3) + | |
scale_y_continuous(breaks = NULL) + | |
theme_classic() + | |
ylab("") + xlab("") | |
p1a | |
MyDF %>% | |
mutate(y_cdf = cumsum(y)) -> MyDF | |
MyDF %>% | |
filter(x %in% c(-3, -2.58, -2, -1.65, -1, -.5, 0, .5, 1, 1.65, 2, 2.58, 3)) -> MyDF_filtered | |
p1a + geom_text(data = MyDF_filtered, | |
aes(x = x, y = y + .1, label = paste(round(y_cdf, 0),"%")), | |
check_overlap = TRUE) + | |
geom_segment(data = MyDF_filtered, | |
aes(x = x, xend = x, y = 0, yend = y), linetype = "dashed") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment