Lab Worksheet 4

Part 1: The dplyr pipe

The following questions in Part 1 are from Lab Worksheet 3. Answer these questions again, but this time use the dplyr pipe (%>%) in your answer.

Problem 1: In an in-class exercise, we made the following plot of the Sitka dataset:

# download the sitka data set:
sitka <- read.csv("http://wilkelab.org/classes/SDS348/data_sets/sitka.csv")
head(sitka)
##   size Time tree treat
## 1 4.51  152    1 ozone
## 2 4.98  174    1 ozone
## 3 5.41  201    1 ozone
## 4 5.90  227    1 ozone
## 5 6.15  258    1 ozone
## 6 4.24  152    2 ozone
ggplot(sitka, aes(x=Time, y=size, group=tree)) + geom_line() + facet_wrap(~treat)

Now modify the plot so that the line for each tree is colored according to the maximum size of the tree.

sitka %>% group_by(tree) %>% mutate(max_size = max(size)) -> sitka_new
ggplot(sitka_new, aes(x=Time, y=size, group=tree, color=max_size)) + geom_line() + facet_wrap(~treat)