Homework #4: Data visualization

Setup the data and libraries

library(plyr)
library(xtable)
library(lattice)
library(directlabels)
## Loading required package: grid Loading required package: quadprog
# Import the Gapminder dataset
gDat <- read.delim("gapminderDataFiveYear.txt")

Part 1: Examining how the range of GDP values changes over time

GDP.table <- ddply(gDat, ~ continent*year, summarize, "Range.of.GDP" = ((max(gdpPercap)-min(gdpPercap))))
stripplot(Range.of.GDP ~ as.factor(year), GDP.table, groups = continent, type = c("p", "a"), auto.key = TRUE)

plot of chunk part2

Part 2: Examining change in GDP and life expectancy

yearMin <- min(gDat$year)
jFun <- function(x) {
   LE.Fit <- lm(lifeExp ~ I(year - yearMin), x)
   GDP.Fit <- lm(gdpPercap ~ I(year - yearMin), x)
   LE.Coef <- coef(LE.Fit)
   GDP.Coef <- coef(GDP.Fit)
   names(LE.Coef) <- NULL
   names(GDP.Coef) <- NULL
   return(c(Life.Expectancy.slope = LE.Coef[2],
            GDP.slope = GDP.Coef[2]
            ))
}
slopes <- ddply(gDat, ~country + continent, jFun)
xyplot(Life.Expectancy.slope ~ GDP.slope, slopes, 
       group = continent, auto.key=TRUE, 
       panel = function(x, y, subscripts, groups, ...) { 
         panel.xyplot(x, y, subscripts = subscripts, 
                      groups = groups)
         panel.abline(v = 0, h =0)
  }
)

plot of chunk part5