First, import data and check it's okay.
library(lattice)
library(plyr)
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 ...
Next, data aggregation check for two fellow students' homework3.
f <- data.frame(ddply(gDat, ~continent + year, summarize, avgLifeExp = mean(lifeExp),
avgGDP = mean(gdpPercap)))
xyplot(avgGDP ~ year, f, groups = continent, auto.key = TRUE, type = c("p",
"a"))
lowmark <- f$avgLifeExp
stripplot(lifeExp ~ factor(year) | reorder(continent, lifeExp), gDat, grid = "h",
group = lifeExp <= lowmark, main = paste("Below average life expectancy",
lowmark), scales = list(x = list(rot = c(45, 0))))