Mosaic Plots with Percentage Labels

FacebookTwitterGoogle+RedditLinkedIn

SOURCEOFINTEL.pngMichael Friendly’s book “Visualizing Categorical Data” has many great examples of visually representing cross tabs. An R package that emerged from that book is the vcd package for making mosaic plots.┬áSomething I could not find an example of, however, was how to use the elaborate struct-plot framework to overlay percentages on each tile. What I came up with is hand rolling a structure object for the purpose. You end up with something like in this example,

 

install.packages("vcd", dependencies=TRUE)
library(vcd)
crosstab <- table(variable1 , variable2)
crosstab <- crosstab[,] #Reorder or and subset rows

#If Row Proportions are wanted later
proportions <- round(prop.table(crosstab , 2 )*100)

#Hand role the structure for the mosaic
values <- c(crosstab)
rowvarcat <- c("A","B")
columnvarcat <- c("1","2")
names=c("Variable Name 1", "Variable Name 2")
dims <- c(2,2) #columns then rows

TABS <- structure( c(values), .Dim = as.integer(dims), .Dimnames = structure( list(rowvarcat,columnvarcat ),
.Names = c(names) ) , class = "table") 

PROPORTIONS <- structure( c(proportions), .Dim = as.integer(dims), .Dimnames = structure( list(rowvarcat,columnvarcat ),
.Names = c(names) ) , class = "table") 

TABSPROPORTIONS <- structure( c(paste(proportions,"%","\n", "(",values,")",sep="")), .Dim = as.integer(dims), .Dimnames = structure( list(rowvarcat,columnvarcat ),
.Names = c(names) ) , class = "table") 

mosaic(TABS,pop=FALSE, main="Plot Title", sub="Plot Subtitle)")
labeling_cells(text=TABSPROPORTIONS , clip_cells=FALSE)(TABS )
FacebookTwitterGoogle+RedditLinkedIn

Leave a Reply

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>