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:
- Create a scatterplot of the number of crossings per day
volumeagainst the high temperature that day - Separate your plot into facets by
weekday - 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.
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!