STAT 1261/2260: Principles of Data Science



STAT 1261/2260: Principles of Data ScienceLecture 4 - ggplot2 (1/3): In-Class ExercisesFather’s Height vs.?Children’s heightUse the famous Galton data set from the mosaic package.Load the mosaic package and check the data set using head function.library(mosaic)head(Galton)## family father mother sex height nkids## 1 1 78.5 67.0 M 73.2 4## 2 1 78.5 67.0 F 69.2 4## 3 1 78.5 67.0 F 69.0 4## 4 1 78.5 67.0 F 69.0 4## 5 2 75.5 66.5 M 73.5 4## 6 2 75.5 66.5 M 72.5 4Exercises:Using ggplot function to create a scatterplot of each person’s height against their father’s height.Separate your plot into facets by sex.Add regression lines to all of your facets.Instead of using facets, now use color hue to indicate sex. Add regression lines for both genders.Part 1:?Create a scatterplotCreate a scatterplot of each person’s height against their father’s height.ggplot(data=Galton,mapping=aes(x=father,y=height))+ geom_point()Part 2:?Examine the relationship for each genderSeparate your plot into facets by sex.ggplot(data=Galton,mapping=aes(x=father,y=height))+ geom_point()+ facet_wrap(~sex)Part 3:?Add linear regression linesAdd regression lines to both of your facets.Don’t remember what “geom function” you should use? check the cheat-sheet through “Help->Cheatsheets->ggplot2”.ggplot(data=Galton,mapping=aes(x=father,y=height))+ geom_point()+ geom_smooth(method="lm")+ facet_wrap(~sex)Part 4: Use the Color HueInstead of using facets, now use color hue to indicate sex. Add regression lines for both genders.ggplot(data=Galton,mapping=aes(x=father,y=height,color=sex))+ geom_point()+ geom_smooth(method="lm") ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download