Homework 5 - ggplot2

Note to Jenny: i am not sure if the boxplot is doing what's intended - i wanted to show boxplot for each year surveyed. However, it's clumped all together?

First, import gapminder data, and drop Oceania.

library(ggplot2)
gap <- "http://www.stat.ubc.ca/~jenny/notOcto/STAT545A/examples/gapminder/data/gapminderDataFiveYear.txt"
gDat <- read.delim(file = gap)
gDat <- droplevels(subset(gDat, continent != "Oceania"))
str(gDat)
## 'data.frame':    1680 obs. of  6 variables:
##  $ country  : Factor w/ 140 levels "Afghanistan",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ year     : int  1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##  $ pop      : num  8425333 9240934 10267083 11537966 13079460 ...
##  $ continent: Factor w/ 4 levels "Africa","Americas",..: 3 3 3 3 3 3 3 3 3 3 ...
##  $ lifeExp  : num  28.8 30.3 32 34 36.1 ...
##  $ gdpPercap: num  779 821 853 836 740 ...

Then produce a scatter plot showing life Expectancy in all continents.

g <- ggplot(gDat, aes(x = lifeExp, y = gdpPercap))
p <- g + geom_point(aes(color = continent)) + scale_y_log10()

Next, do a strip plot and then a boxplot for life expectancy across years, using multipanel:

p + facet_wrap(~continent)

plot of chunk unnamed-chunk-3


y <- ggplot(gDat, aes(x = year, y = lifeExp))
y + layer(geom = "point")

plot of chunk unnamed-chunk-3


y + geom_boxplot(aes(fill = continent)) + facet_wrap(~continent, ncol = 3)

plot of chunk unnamed-chunk-3