Skip to content

Instantly share code, notes, and snippets.

@seankross
Created March 3, 2016 23:01
Show Gist options
  • Save seankross/7e25203bca4db9ceb856 to your computer and use it in GitHub Desktop.
Save seankross/7e25203bca4db9ceb856 to your computer and use it in GitHub Desktop.
Draw normal distribution on histogram of any dataset
# 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