R tips/Contingency Tables

From QERM Wiki
Jump to: navigation, search
Mosaic plot of Steller sea lion female birth probabilities.

Plotting contingency data

Along with the convoluted bubble plot that we constructed in lab (QERM 598, Week 5), a student revealed several powerful visualizations of contingency data that R offers. These are: mosaicplot(table(Cat1,Cat2)) and assocplot(table(Cat1,Cat2)) where "Cat1" and "Cat2" are two categorical vectors that are tabulated in "contingency" format. These are very useful, easy functions for visualization!

The mosaic plot on the right is busy, but it shows a way to present three categorical counts simultaneously: Age of females (from 4 to 18 years along the x-axis), Rookery (darker to lighter boxes represent four rookeries, from southermost Chirpoev to northern most Antsiferov) and the real variable of interest: Birth/No Birth (thistle and yellow colors). The width of the boxes is proportional to the number of observations.

The data is here: Data.png SealionBirths.dat. The plot is generated with the following four lines of code (no need to download the data if you're connected to the internets).

Births<-read.table("http://wiki.cbr.washington.edu/qerm/images/d/d3/SealionBirths.dat",header=T)
attach(Births)
mycolors <- c("yellow1","yellow2","yellow3","yellow4", "thistle1","thistle2","thistle3","thistle4")
mosaicplot(table(Age,paste(Birth,Island)),col=mycolors,main="")

Eli 14:46, 16 May 2008 (PDT)

Personal tools