Skip to content

Instantly share code, notes, and snippets.

@kissge
Created July 26, 2014 12:53
Show Gist options
  • Save kissge/850f5b67c1fa11e02a1e to your computer and use it in GitHub Desktop.
Save kissge/850f5b67c1fa11e02a1e to your computer and use it in GitHub Desktop.
Create a histogram from frequency list
hist.from.freq <- function (range, freq, normalize = F, main = "Histogram") {
wid <- s[2] - s[1]
range <- range - wid / 2
range[length(range) + 1] <- range[length(range)] + wid
if (normalize) freq <- freq / sum(freq)
dat <- list(breaks = range, counts = freq)
attr(dat, "class") <- "histogram"
plot(dat, main = main, ylab = ifelse(normalize, "Probability", "Frequency"))
}
# usage:
# hist.from.freq(seq(0, 9), c(154, 105, 72, 16, 111, 103, 81, 84, 38, 158), normalize = T)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment