Pages

Monday, April 9, 2012

Contested Posessions Plot Using ggplot2


Of interest to the R user (but unfortunately, not to the follower of footy ) is how to reproduce the Contested Possessions box-plot using ggplot2. Now ggplot2 is a well known package in R - ggplot2 is a powerful and flexible system for creating chart graphics - it's promise is extra flexibility and power than one gets with the charting functions in Base R.

ggplot2 has two options:

- qplot - this is the quick plotting function
- ggplot2 - this is the full plotting function, allowing almost unlimited customisation


My aim is to produce the box-plots initially using qplot, and then to recreate the plot in ggplot2.

This code produces the box-plot using qplot:

byafl <- with(afl, reorder(V2,V1))

qplot(byafl,V3, data = afl, geom = "boxplot", xlab = "Teams - Ladder Order", ylab = "Contested Possessions Per Game", main = "Contested Posessions 2011")


The equivalent code using ggplot2 is:

ggplot(afl, aes(byafl,V3))+ geom_boxplot() + scale_x_discrete("Teams - Ladder Order") + scale_y_continuous("Contested Possessions Per game") + opts(title = "Contested Possessions 2011")


Here is the plot using ggplo2 - is there any difference ?



No comments:

Post a Comment