Last active
December 29, 2015 02:09
-
-
Save kjgarza/7598491 to your computer and use it in GitHub Desktop.
Country Comparison :: Obesity - adult prevalence rate.
Data: https://www.cia.gov/library/publications/the-world-factbook/rankorder/rawdata_2228.txt
Based on: http://www.devjason.com/2009/04/28/more-quick-r-maps-country-view-of-who-confirmed-cases/
Based on: http://www.r-bloggers.com/r-using-rcolorbrewer-to-colour-your-figures-in-r-2/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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