Faceted histogram

In this chapter, you'll be working with the cars dataset, which records characteristics on all of the new models of cars for sale in the US in a certain year. You will investigate the distribution of mileage across a categorical variable, but before you get there, you'll want to familiarize yourself with the dataset.

This exercise is part of the course

Exploratory Data Analysis in R

View Course

Exercise instructions

The cars dataset has been loaded in your workspace.

  • Load the ggplot2 package.
  • View the size of the data and the variable types using str().
  • Plot a histogram of city_mpg faceted by suv, a logical variable indicating whether the car is an SUV or not.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Load package


# Learn data structure


# Create faceted histogram
ggplot(cars, aes(x = ___)) +
  geom_histogram() +
  facet_wrap(~ ___)