Last active
September 20, 2016 04:03
-
-
Save tjmahr/2594bea38ec71419cde46f677e11bb78 to your computer and use it in GitHub Desktop.
Density curve annotations
This file contains hidden or 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
library("ggplot2") | |
library("dplyr") | |
# Some toy data | |
davis <- car::Davis %>% | |
filter(100 < height) | |
# Fit a model and estimate mean at five points | |
m <- lm(weight ~ height, davis) | |
newdata <- data_frame(height = c(15:19 * 10)) | |
newdata$fit <- predict(m, newdata) | |
# get density of random normal values | |
get_density_df <- function(mean, sd, steps) { | |
ends <- qnorm(c(.001, .999), mean, sd) | |
steps <- seq(ends[1], ends[2], length.out = steps) | |
df <- data_frame( | |
value = steps, | |
density = dnorm(steps, mean, sd)) | |
df | |
} | |
# Get a distribution at each mean | |
simulated <- newdata %>% | |
group_by(height) %>% | |
do(get_density_df(.$fit, sigma(m), 10000)) %>% | |
ungroup | |
ggplot(simulated) + | |
# Plot at each mean, adding some scaled value of density to the mean. | |
aes(x = height - (100 * density), y = value, group = height) + | |
geom_polygon(fill = "grey50") + | |
# raw data | |
geom_point(aes(height, weight), data = davis) | |
ggplot(simulated) + | |
aes(x = height + (150 * density), y = value, group = height) + | |
geom_polygon(fill = "grey80", color = "skyblue") + | |
geom_point(aes(height, weight), data = davis) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment