Histograms

The basic plot and making adjustments

Histograms are one of the most straightforward and useful ways to visualize the distribution of a numeric variable. A histogram shows the distribution of a single numeric variable by dividing its range into bins (intervals), counting how many observations fall into each bin, and visualizing these counts as bars in a plot. Histograms are meant to represent a variable’s probability distribution, so they’re useful for understanding the shape, spread, and skewness of numeric variables.

The hist() function in Base R creates histograms.

Do: Run the code below.

hist(diabetes$glu)

See: The code visualizes the distribution of blood glucose levels in the diabetes dataset. Blood glucose measurements in a general patient population tend to cluster around a normal range, with a smaller number of patients showing elevated readings. You can see that this distribution is right-skwed: most glucose readings cluster at the lower end of the range, with a longer tail stretching toward higher, less common values.

By default, hist() will try to best represent the distribution by applying the Sturges Formula to define the bin width. We can alter this behavior in several ways through the breaks argument.

  • Specify a different built-in algorithm
  • Provide a vector of cut points giving the bin margins
  • Request a fixed number of bins
  • Define our own function to create the bins

Predict: What will happen to the histogram of blood glucose as we go from very few bins (5) to very many bins (50). Will the distribution’s shape become clearer or noisier?

hist(diabetes$glu,
     main = "Define bins via Scott's formula",
     breaks = "scott")

hist(diabetes$glu,
     main = "Define bins with a vector",
     breaks = seq(50, 130, 10))

hist(diabetes$glu, main = "Ask for 5 bins", breaks = 5)

hist(diabetes$glu, main = "Ask for 50 bins", breaks = 50)

Explain: With only 5 bins, the histogram is coarse: it still shows the right skew, but hides finer details about where exactly the data cluster. With 50 bins, individual bars become sparse and jagged, since each bin now contains only a handful of observations, and this bin-to-bin noise can look like “real” bumps in the distribution that aren’t actually meaningful. Choosing a sensible number of bins is therefore a balance between hiding structure (too few bins) and amplifying random noise (too many bins).

Customizing the plot

Do: You can customize the appearance of the plot in many ways, using the many parameters that the hist() function accepts. Some examples of parameters that can be adjusted are:

  • main: Add a better title
  • col: Change the fill color of the bars
  • border: Change the border color of the bars
  • probability: Switch the y-axis to a probability scale

See: By running the code below and comparing the new plot to the previous one, you can see how the changes are implemented in practice:

hist(diabetes$glu,
     main = "Colorful Histogram",
     col = "pink",
     border = "blue",
     probability = FALSE)

Predict: How will the y-axis change once we set probability = TRUE instead of leaving it at its default. Will the bars themselves change shape, or just what the y-axis represents?

hist(diabetes$glu,
     main = "Colorful Histogram",
     col = "pink",
     border = "blue",
     probability = TRUE)

Explain: As you can see, the bars are exactly the same shape as before, only the y-axis has changed. Setting probability = TRUE converts the y-axis to a probability scale and makes the histogram’s total area sum to 1. Hence the bar heights represent the proportion of the sample that falls into each bin, rather than a straight count of the observations in each bin.

Graphical Parameters

External Resources

The tutorial linked below gives a nice overview of the various graphical parameters you can use to adjust the appearance of Base R visualizations.

STHDA Tutorial: R Graphical Parameters

The adjustments you see above are only a tiny drop in a vast bucket of possibilities. We can tweak nearly every aspect of our visualization’s appearance by adjusting the values of the built-in graphical parameters. There are many such parameters. To see a full list of the graphical parameters, read the documentation for the par() function.

?par
Practice exercise

Make a histogram showing the age distribution in the diabetes dataset.

  • Adjust the bin width to create a fine-grained visualization with many, small bins
  • Set the fill-color of the columns to green
  • Add a better label for the x-axis
  • Add a better plot title

NOTE: The diabetes dataset is already loaded in the working directory of this webr session.

hist(diabetes$age,
     col = "green",
     breaks = 50,
     xlab = "Age",
     main = "Age Distribution")

Back to top