Skip to content

Instantly share code, notes, and snippets.

@kjgarza
Last active December 29, 2015 02:09
Show Gist options
  • Save kjgarza/7598491 to your computer and use it in GitHub Desktop.
Save kjgarza/7598491 to your computer and use it in GitHub Desktop.
# load required libraries
require(maps)
require(RColorBrewer)
# Create a dataframe with the reported observations
# get data from https://www.cia.gov/library/publications/the-world-factbook/rankorder/rawdata_2228.txt
# transform it to csv
r <- read.csv(file=paste0("/Dropbox/Data/rawdata_2228.csv"),sep=",",head=FALSE)
loc<-r$V2
cases<-r$V3
flu <- data.frame(loc,cases)
# Setup the coordinate system
png("example.png",width=60,height=60,units="in",res=75)
m <- map('world',plot=TRUE, fill=T, resolution=2)
# Match up our observations
stm <- match.map(m, flu$loc)
# 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')
pal <- colorRampPalette(brewer.pal(9,"YlGnBu"))(max(flu$rank))
color <- pal[flu$rank]
flu.color <- color[stm]
# Do the drawing
map(m,col=flu.color,fill=T, lty=0,boundary=F,interior=F) # fill regions
map('world',interior=T,add=T,col='grey30', exact=FALSE) # plot boundaries
map.axes()
grid(col='grey50')
title('Country Comparison :: \n Obesity - adult prevalence rate (2008)')
graphics.off()
# legend('bottomleft', legend=paste(flu$loc,flu$cases),
# fill=color, bg='white', horiz=T, cex=0.75,
# title=paste('Jason B. Smith | 28 April 2009 | Source: CNN.com'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment