You are on page 1of 2

Examining Distributions Series

Topic #7: Using R to Construct a Histogram

Let’s look at how we can use R to construct a histogram. I have opened RStudio
and have prepared a script for us to use. Histograms are for use with quantitative
data. I have been analyzing data from the Titanic disaster, so continuing with that
dataset, let’s look at the distribution of passenger ages.

As always, I need to make sure that my data are read into an R object, so I’ll do
that now.

We use the “hist” function to create a histogram. I am using three arguments,


which is all you need to create a basic histogram. First, we tell the hist function
what variable we want to display. The age variable is in the Titanic object, so I am
going to use the syntax Titanic, dollar sign, age. Next, I label the horizontal axis as
age, in years. Quantitative data involve a level of measurement, so we should
indicate the level of measurement somewhere on our graph. It can be on the
appropriate axis, as I have done here, or it could be in the title.

Speaking of titles, let’s give this graph a title using the argument “main.” Sensibly
enough, I titled this, “ages of passengers on the Titanic.”

Let’s make sure that our files and plots pane is a good size for displaying this
graph, then run the hist function.

Here’s the histogram. The mode is in the low 20s. There are some children on
board, but not too many people in their 70s and 80s, so that the distribution has a
slight positive skew.

1
This graph is now ready to export for use in a report of our data analysis. Pretty
simple, isn’t it? Go give it a try!

You might also like