Histogram

To visualize the distribution of a set of numbers, you can plot a histogram. If you keep studying statistics, you will learn that a histogram is a statistical estimator of a probability density function. Neat!

Anyway, this is super simple in R. Here is a vector of random numbers:

set.seed(1)
x <- rgamma(5000, shape = 2, rate = 1)

To plot a histogram, use the hist command:

hist(x, breaks = "Scott", freq = FALSE,
     main = "My histogram",
     xlab = "x values",
     ylab = "density", 
     col = "lightblue", 
     border = "white")

The hist command has many arguments, and you can read the documentation for more detail. Apart from the cosmetic stuff controlling the labeling and the color, there are three main arguments to think about:

hist(x, breaks = "Scott", freq = FALSE)
curve(dgamma(x, 2, 1), from = min(x), to = max(x), n = 1000, 
      col = "red", lwd = 3, add = TRUE)