the motivation...

ggplot2 is awesome! I especially love how pretty and neat maps look. Unfortunately, ggplot2 is still left outside of most intro to stats classrooms... So, in collaboration with Prof. Randy Pruim (Calvin College), I developed a few functions that use ggplot2 to create country-level and US state-level maps very easily. These functions have now been integrated in the MOSAIC R package, often used in intro classes. You can install the newest version of MOSAIC directly from CRAN.

the goods...

Here's the main idea: say you have a dataset, called women, that starts like this (this is actual data -- see the note at the end of the page for a link):

state female f_single f_married f_divorced
Alabama 2027167 537885 934656 277280
Alaska 274355 78101 136624 36556
Arizona 2632694 766043 1213599 369422
Arkansas 1212609 287878 586304 176458
California 15397459 5092645 6904534 1732495
Colorado 2079190 566395 1035632 292065
Connecticut 1526166 470937 706508 185613
Delaware 389090 118858 175329 51792
District of Columbia 287057 160809 71623 28605
Florida 8259052 2290565 3616605 1211819
Georgia 4046447 1226048 1825066 525291
Hawaii 567447 164861 279318 57365


After installing and loading mosaic, you only need one line of code to produce the following plot:

mUSMap(data=women, key="state", fill="log(f_divorced)")

Would you prefer to see Alaska and Hawaii in their proper places:

mUSMap(data=women, key="state", fill="log(f_divorced)", style="real")

A similar function ( mWorldMap() ) exists to make country-level data.


for more...

For more information about MOSAIC, visit the documentation and vignettes. If you are interested in a step by step document that goes over the map capabilities of MOSAIC, you might find this useful. It was written assuming very little familiarity with R, and so it is suitable for absolute beginners and intro students. If you want to follow along you will need these data files.