Introduction

These exercises are taken mostly from the grammar of graphics chapter from Modern Data Science with R: http://mdsr-book.github.io.

I liked the Railtrail data.

Railtrails

Using the RailTrail data set from the mosaicData package:

  1. Create a scatterplot of the number of crossings per day volume against the high temperature that day
  2. Separate your plot into facets by weekday
  3. Add regression lines to the two facets

SOLUTION:

ggplot(RailTrail, aes(x = hightemp, y = volume)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE) +
  facet_wrap(~ weekday, nrow = 2) +
  labs(x = "high temperature (deg. F)",
       y = "trail volume")
Traffic along a trail.  Each point is a day in the year.

Traffic along a trail. Each point is a day in the year.

Some folks wondered how to get rid of the TRUE and FALSE labels at the top of each plot. Here’s one way:

RailTrail %>% 
  mutate(weekday_nice = ifelse(weekday, "weekday", "weekend")) %>% 
  ggplot(aes(x = hightemp, y = volume)) +
    geom_point() +
    geom_smooth(method = "lm", se = FALSE) +
    facet_wrap(~ weekday_nice, nrow = 1) +
    labs(x = "high temperature (deg. F)",
         y = "trail volume")
same as before, but with better facet-labels!

same as before, but with better facet-labels!