# Read in data
myData = read.delim(file = "http://www.kktg.net/R/Chapter12Data.txt",
colClasses = c("character", "numeric", "numeric", "numeric"), header = T)
# Create categorical versions of the dem/polity data
myData$dem =
ifelse(myData$P4 > 7, 1, 0) # Dem if Polity >7
myData$corrupt =
ifelse(myData$CPI < 4, 1, 0) # Corrupt if CPI<4
myData$cGDPk = myData$cGDP/1000 # Per capita GDP in 1000s
# Remove all observations with missing data
myData = myData[!is.na(myData$CPI) & !is.na(myData$P4),]
# 12.8 Mosaic Plots ======================================================= 12.8
# There are two steps here. First we have to transform the data into a
# contingency table. This is an array that shows the frequency of observations
# at each combination of the variables of interest.
# A simple 2x2 table
myTable = table( # Create a contingency table
myData$dem, # with x = dem/not dem
myData$corrupt) # and y = corrupt/not corrupt
myTable # Display contingency table
png(filename = "illustrations/fig-12-16-mosaicplot1.png",
units = "in", # Set measurements in inches
res = 1200, # Set resolution at 1200dpi
width = 6, # Width at 6 inches
height = 4) # Height at 4 inches
par(mai = c(.5, .5, .5, .25)) # Change margins for png graphs
mosaicplot(myTable, # Create mosaic plot for myTable
main = "Democracy & Corruption", # Set main title
xlab = "Dem", # Set x axis label
ylab = "Corrupt") # Set y axis label
dev.off() # Output png file
Figure 12-16: A Simple Mosaic Plot
Portfolio Categories: All Graphics and SGR Book Graphics.