# Tabulate the counts of each ticket class
classTable <- table(titanic$class)
# Create a bar plot to visualize the frequency of ticket classes
barplot(classTable)
Bar plots are the analogue of histograms for discrete variables. Whereas the columns in a histogram are defined by counting the number of cases that fall into arbitrary bins, the bars in a bar plots represent the number of cases that belong to each level of a categorical variable.
We use the barplot() function to create bar plots in Base R. As with kernel density plots, however, we first need to summarize the data before creating the visualization. Specifically, we use the table() function to create a frequency table, and we then plot that table with barplot().
Do: Run the following code that visualizes the distribution of ticket classes in the titanic dataset. On a ship like the Titanic, third class tickets were generally the cheapest and most numerous. Before running the code, think about which ticket class you’d expect to have the tallest bar.
# Tabulate the counts of each ticket class
classTable <- table(titanic$class)
# Create a bar plot to visualize the frequency of ticket classes
barplot(classTable)
See: In the plot we see that the third class has the tallest bar by a wide margin, reflecting that most passengers travelled in the cheapest ticket class.
Predict: To plot the counts as proportions, we convert the data in the table before plotting. Predict how this will change the bar plot: will the bars change height relative to each other, or just the numbers shown on the y-axis?
# Convert counts to proportions
classProps <- classTable / sum(classTable)
# Create a bar plot to visualize the frequency of ticket classes
barplot(classProps)
Explain: As with the switch from counts to a probability scale in a histogram, the bars keep exactly the same relative heights; only the y-axis has changed, now showing the proportion of passengers in each class instead of the raw count.
As with all of our Base R plotting, we can tweak the appearance in many ways. For example, setting horiz = TRUE flips the orientation of the bars. Which axis will now show the ticket classes, and which will show the counts?
barplot(classTable,
horiz = TRUE,
main = "Classes of Titanic Tickets",
ylab = "Ticket Class",
xlab = "Number of Passengers",
col = rgb(181, 137, 0, max = 255),
border = rgb(0, 43, 54, max = 255))
The ticket classes now run along the y-axis, and the bars extend horizontally to show the passenger counts along the x-axis; the underlying data haven’t changed, only the layout of the plot.
Bar plots and histograms can look similar at a glance, but they visualize different kinds of variables. A bar plot’s bars are separated by gaps because each bar represents a distinct, unordered or ordered category (e.g., a ticket class); there’s no meaningful “in-between” value connecting one bar to the next. A histogram’s bars touch because they represent contiguous bins across a continuous range, where every value in between is meaningful. Using a bar plot for a continuous numeric variable (or a histogram for a categorical one) misrepresents the nature of the data.
Use the titanic data to create a bar plot showing the proportion of passengers of each sex (sex).
NOTE: The titanic dataset is already loaded in the working directory of this webr session.