Using R for Quick Mapping of Swine Flu
![]()
I haven’t had much experience using R for spatial visualization, so I thought I would give the “maps” packages a go tonight and create a quick thematic map of confirmed cases of swine flue by state. It doesn’t have all the elements I would want on a production map, but I was going for speed of generation.

Here is the R code I used to generate the map.
draw.flumap <- function() {
# load required libraries
require(maps)
require(RColorBrewer)
# Create a dataframe with the reported observations
states <- c('California','Kansas','New York','Ohio','Texas')
cases <- c(7,2,28,1,2)
flu <- data.frame(states,cases)
# Match up our observations with the "state" database
stm <- match.map("state", states)
# Rank the cases and assign colors using the RColorBrewer YlOrRd palette
flu$rank <- rank(flu$cases, ties="min")
pal <- brewer.pal(max(flu$rank),"YlOrRd")
color <- pal[flu$rank]
flu.color <- color[stm]
# Actually do the drawing
map("state",proj="albers",col=flu.color, parameters=c(30,40),fill=T,lwd=0.7)
title("US Confirmed Cases of Swine Flu by State (27 April 2009)")
legend('bottomleft', legend=paste(flu$states,flu$cases),fill=color, cex=0.75)
text(0.26, -1.62, labels=paste("Albers Equal Area\n","Jason B. Smith\n", "27 April 2009"), cex=0.75)
}
# Just saves it out as a file
png(filename="~/Desktop/flu.png", width=600, height=500, bg="white")
print(draw.flumap())
dev.off()
The draw.flumap function has everything you would need to do this in an interactive R console, I really just wrapped it at the end to make it easier to save the image to a file. I had to make use of the locator() function for placement of the text.