Created
March 3, 2016 23:01
-
-
Save seankross/7e25203bca4db9ceb856 to your computer and use it in GitHub Desktop.
Draw normal distribution on histogram of any dataset
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
# Draw a normal distribution curve on top of a histogram | |
# of any vector | |
# | |
# @param data A numeric vector | |
hist_and_norm <- function(data){ | |
hist_min <- mean(data) - 4*sd(data) | |
hist_max <- mean(data) + 4*sd(data) | |
normalx <- seq(hist_min, hist_max, by = 1) | |
normaly <- dnorm(normalx, mean = mean(data), sd = sd(data)) | |
hist(rvs, xlim = c(hist_min, hist_max), ylim =c(0, max(normaly)), | |
freq = FALSE) | |
lines(normalx, normaly, col = "red") | |
} | |
rvs <- sample(1:1000, replace = TRUE) | |
hist_and_norm(rvs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment