Homework 1

Exercise 1

library(MASS)
library(ggplot2)
library(gridExtra)
## Loading required package: grid
default <- ggplot(survey, aes(factor(Smoke), fill = factor(Smoke))) + geom_bar() + 
    labs(title = "Smoking in College Students - Default", x = "", y = "Count", 
        fill = "Smoking level") + theme(plot.title = element_text(size = rel(2)))
survey$Smoke = factor(survey$Smoke, levels = c("Never", "Occas", "Regul", "Heavy"))
ordered <- ggplot(survey, aes(factor(Smoke), fill = factor(Smoke))) + geom_bar() + 
    labs(title = "Smoking in College Students - Ordered", x = "", fill = "Smoking level") + 
    theme(plot.title = element_text(size = rel(2)))
grid.arrange(default, ordered, ncol = 2)

plot of chunk unnamed-chunk-2

default <- ggplot(survey, aes(factor(Exer), fill = factor(Exer))) + geom_bar() + 
    labs(title = "Exercising in College Students - Default", x = "", y = "Count", 
        fill = "Exercise level") + theme(plot.title = element_text(size = rel(2))) + 
    coord_flip()
survey$Exer = factor(survey$Exer, levels = c("None", "Some", "Freq"))
ordered <- ggplot(survey, aes(factor(Exer), fill = factor(Exer))) + geom_bar() + 
    labs(title = "Exercising in College Students - Ordered", x = "", fill = "Exercise level") + 
    theme(plot.title = element_text(size = rel(2))) + coord_flip()
grid.arrange(default, ordered, ncol = 2)

plot of chunk unnamed-chunk-3

Describe how your graphs display the distributions of how much/often the students smoke and exercise. What e ect does ordering the categories have on the graphic? What information do you gain (if any)? Which graphic do you prefer? Why?

Exercise 2

raceplot <- ggplot(birthwt, aes(factor(race), fill = factor(smoke))) + geom_bar() + 
    labs(title = "Race in newborns", x = "Race", y = "Count", fill = "Smoking level") + 
    theme(plot.title = element_text(size = rel(2)))
smokeplot <- ggplot(birthwt, aes(factor(smoke), fill = factor(ptl))) + geom_bar() + 
    labs(title = "Smoking habits of newborns moms", x = "Smoking level", y = "Count", 
        fill = "PTL") + theme(plot.title = element_text(size = rel(2)))
ptlplot <- ggplot(birthwt, aes(factor(ptl), fill = factor(race))) + geom_bar() + 
    labs(title = "PTL in newborns", x = "PTL", y = "Count", fill = "Smoking level") + 
    theme(plot.title = element_text(size = rel(2)))
grid.arrange(raceplot, smokeplot, ptlplot, ncol = 3)

plot of chunk unnamed-chunk-4

par(mfrow = c(1, 3))
racesmoke <- table(birthwt[, c("race", "smoke")])
mosaicplot(racesmoke, shade = TRUE, main = "Race vs Smoking moms")
smokeptl <- table(birthwt[, c("ptl", "smoke")])
mosaicplot(smokeptl, shade = TRUE, main = "PTL vs Smoking moms")
ptlrace <- table(birthwt[, c("ptl", "race")])
mosaicplot(ptlrace, shade = TRUE, main = "PTL vs Race")

plot of chunk unnamed-chunk-5