hist(diabetes$glu)
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.
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.
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?




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).
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 titlecol: Change the fill color of the barsborder: Change the border color of the barsprobability: Switch the y-axis to a probability scaleSee: By running the code below and comparing the new plot to the previous one, you can see how the changes are implemented in practice:
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?
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.
The tutorial linked below gives a nice overview of the various graphical parameters you can use to adjust the appearance of Base R visualizations.
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.
Make a histogram showing the age distribution in the diabetes dataset.
NOTE: The diabetes dataset is already loaded in the working directory of this webr session.