Lab 2, September 19
To test your knowledge of ggplot geoms, complete Exercise 3.6.1 Number 6 from the textbook. You are asked to recreate the following plots using the
mpg data frame.
This exercise allows you to practice statistical transformations and position adjustments in ggplot. We will use the
ChickWeight data set, which is automatically loaded when you start an R session.
Run the command
?ChickWeight to learn about how the data were collected.
We would like to understand how the different diets affected the chicks’ weight over time.
Let’s start by plotting the growth trajectory of each chick over time.
ggplot(ChickWeight)+ geom_line(aes(x=Time, y=weight, group=Chick, color=Diet))
It might be easier to compare the four diets by computing the average chick weight at each time point.
Recreate the following plot. Each line plots the mean chick weight over time for each diet. The points display the individual chick weights over time.
stat_summaryto plot the mean weight for each diet over time.
position_jitterto move the points slightly to the left and right. Do not adjust the vertical position of the points.
scale_color_hueto your plot, specifying the
labelsarguments to control the legend.