1. Learn
  2. /
  3. Courses
  4. /
  5. Introduction to Data Visualization with ggplot2

Connected

Exercise

Drawing histograms

Recall that histograms cut up a continuous variable into discrete bins and, by default, maps the internally calculated count variable (the number of observations in each bin) onto the y aesthetic. An internal variable called density can be accessed by using the .. notation, i.e. ..density... Plotting this variable will show the relative frequency, which is the height times the width of each bin.

Instructions 1/4

undefined XP
  • 1
    • Using mtcars, map mpg onto the x aesthetic.
    • Add a histogram layer using geom_histogram().
  • 2
    • Set the histogram binwidth to 1.
  • 3
    • Map y to the internal variable ..density.. to show frequency densities.
  • 4
    • Set the fill color of the histogram bars to datacamp_light_blue.