5.2 Make histograms with base graphics - Video Tutorials & Practice Problems
Video duration:
1m
Play a video:
Video transcript
<v Voiceover>When working with a single</v> continuous variable, histograms are commonly the best form of visualization, and it chops up the variable into buckets and plots the relative frequency of each bucket, sort of like a bar chart. We're going to look at the carat variable from the diamonds data set to illustrate this. Building a histogram in Base R is very simple. It is simply the hist function and then the variable of interest, which in this case is diamond$carat. Running that, we see, over here in the plot area, a histogram of the carat size. So this is showing that the most common carat size is between zero and a half carat, followed by diamonds between a half and one carat, then one to one-and-a-half, and one-and-a-half to two, and it decreases as it goes along, because there are very few large diamonds. Now the axis were labeled automatically, as was the chart itself. To change those, we can once again call hist on the same data, diamonds$carat, but this time we will say main equals Carat Histogram. That will change the title over here, and we'll also change xlab equals carat, so now, down here, the x-axis will be labelled carat, and those changes were easily made.