stat545a-2013-hw05_wang-ton

Install and load the ggplot2 and plyr packages.

install.packages("ggplot2", dependencies = TRUE)
Error: trying to use CRAN without setting a mirror
##install.packages("grid")
library(ggplot2)
library(plyr)
library(lattice)
library(hexbin)

Data import

gdURL <- "http://www.stat.ubc.ca/~jenny/notOcto/STAT545A/examples/gapminder/data/gapminderDataFiveYear.txt"
gDat <- read.delim(file = gdURL)
gDat <- droplevels(subset(gDat, continent!="Oceania"))

Examine “typical” life expectancy for different years.

p <- ggplot(gDat, aes(x = year, y = lifeExp))
p+ geom_point(alpha = 0.4) + geom_smooth(colour="red") ##smooth uses "mean"?

plot of chunk unnamed-chunk-4

How is life expectancy changing over time on different continents? (Here typical life expectancy is defined as trimmed mean, with level of 0.1)

stat_sum_single <- function(fun, geom="point", ...) {
  stat_summary(fun.y=fun, geom="smooth", size = 1, ...)
}
myTrimMean = function (x){
  mean(x,tirm=0.1)
}
ggplot(gDat, aes(x = year, y = lifeExp,color=factor(continent)))+ geom_point(alpha=0.6)+ stat_sum_single(myTrimMean)+facet_wrap(~continent)

plot of chunk unnamed-chunk-5

Explore relationship between life expectancy and gdpPerCap with scatterplot.

ggplot(gDat, aes(x = gdpPercap, y = lifeExp,color=factor(continent)))+ scale_x_log10()+ geom_point(alpha = 0.6) + geom_smooth(se = FALSE) #confidence band off

plot of chunk unnamed-chunk-6

Bin the scatterplot into hexgons (with lattice)

ggplot(gDat, aes(x = gdpPercap, y = lifeExp))+ scale_x_log10()+ geom_hex() + geom_smooth(se = FALSE)+facet_wrap(~continent)

plot of chunk unnamed-chunk-7

Bin the scatterplot into hexgons (with lattice)

hexbinplot(lifeExp ~ gdpPercap|continent, gDat,scales = list(x = list(log = 10)),xbins = 40)

plot of chunk unnamed-chunk-8

Show scatterplot of life expectancy and gdpPercap in differnt pannel according to continent and year.

ggplot(gDat, aes(x = gdpPercap, y = lifeExp,color=factor(continent)))+geom_point(alpha=0.3)+scale_x_log10() + geom_smooth(method=loess,se = FALSE)+ facet_grid(continent~year)

plot of chunk unnamed-chunk-9

Show the spread of GDP per capita within the continents with ggplot2.

ggplot(gDat, aes(x = gdpPercap,color=factor(continent)))+geom_density()+scale_x_log10()

plot of chunk unnamed-chunk-10

Show the spread of GDP per capita within the continents with lattice.

densityplot(~gdpPercap, data =gDat,scales = list(x = list(log = 10)),plot.points=FALSE,ref=TRUE, groups = reorder(continent, gdpPercap), auto.key = TRUE)

plot of chunk unnamed-chunk-11

Depict the number and/or proportion of countries with low life expectancy over time by continent (with ggplot)

bMark = 48.20
proLoLifeExp <- ddply(gDat, ~ continent + year, function(x) 
  {jCount = sum(x$lifeExp <= bMark)
 c(count = jCount, prop = jCount / nrow(x)) })
ggplot(proLoLifeExp, aes(x=year,y=prop,color=factor(continent)))+geom_smooth(method=loess,se=FALSE)

plot of chunk unnamed-chunk-12

Depict the number and/or proportion of countries with low life expectancy over time by continent (with lattice).

xyplot (prop ~ year, group=continent,proLoLifeExp, jitter.data = TRUE,auto.key = TRUE, grid = "h",type="l")

plot of chunk unnamed-chunk-13