Playing with Lattice and ggplot2 on Gapminder Data Set

Data import from URL

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

Load the lattice and plyr packages.

library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.0.2
library(plyr)
## Warning: package 'plyr' was built under R version 3.0.2
library(lattice)
## Warning: package 'lattice' was built under R version 3.0.2

Stripplot-type of figure that shows lifeExp over years for different continent

p <- ggplot(gDat, aes(x = as.factor(year), y = lifeExp,color = continent)) 
p + geom_point()+geom_boxplot()+facet_wrap(~ continent)

Do it in a different way

p <- ggplot(gDat, aes(x = continent, y = lifeExp, color = continent)) 
p + geom_point()+geom_boxplot()+facet_wrap(~year)

Scatterplot that shows correlation between lifeExp and GdpPercap within each continent, also fit a loess model to each continent

p <- ggplot(gDat, aes(x = lifeExp, y = gdpPercap ,color = continent)) 
p + geom_point()+facet_grid(~continent)+geom_smooth(method = "loess" ,colour = "black")

One thing that is good in ggplot but hard in lattice is that when you are showing data in a 2-D space,ggplot geom_bin2d() allows you to show the graph in different colors according to the density of points in a small area.

For illustration, consider gdpPercap vs lifeExp for different continents:

In lattice,it only shows data points:

xyplot(gdpPercap~lifeExp|continent,gDat,scales = list(y = list(log = 10, equispaced.log = FALSE)))

In ggplot, it’s able to show the density:

p <- ggplot(gDat, aes(x = lifeExp, y = gdpPercap)) 
p +facet_wrap(~continent)+geom_bin2d()+scale_y_log10()

Code Style:

For lattice, most likely it’s a one line of code specifying what kind of plot you are using and what you are going to plot

For ggplot you can use as many lines as you want to add as many things(layer,setting) as you want.

Figure result:

For lattice, it produces simple graph with better defaults and it’s easy to display multivariate relationships

For ggplot you can add many layers to the plot and do many panel settings to each layer to make it a complex multi-layered graph.