# Bret Larget
# September 30, 2011
# code to implement mosaic plots using lattice
# this code is not very robust, but should work for the homework assignment.
require(lattice)
# x should be a matrix of counts
# the display will be best if rows and columns of x have suitable names
# Example:
# # enter data column-wise.
# > fish = matrix(c(1,49,10,35,37,9),nrow=2,ncol=3)
# # add row names
# > rownames(fish) = c("Eaten","Not Eaten")
# # add column names
# > colnames(fish) = c("Uninfected","Lightly Infected","Highly Infected")
# # make the mosaic plot
# > mosaic(fish)
mosaic = function(x,...) {
# replace frequencies with relative frequencies in each column
col.sums = apply(x,2,sum)
for(j in 1:ncol(x))
x[,j] = x[,j] / col.sums[j]
my.plot = barchart(t(x), horizontal=F,ylab="Relative Frequency",auto.key=list(columns=nrow(x)),...)
plot(my.plot)
}