Skip to content

Instantly share code, notes, and snippets.

@zemadi
Last active December 20, 2015 15:59
Show Gist options
  • Save zemadi/6158693 to your computer and use it in GitHub Desktop.
Save zemadi/6158693 to your computer and use it in GitHub Desktop.
This is my first successful R project. It's a simple map using data from the Office of the United Nations High Commissioner for Refugees and the World Bank, which shows the percent of each country's population that are refugees.
library(rworldmap)
library(RColorBrewer)
#I had the data as .CSV and loaded it into RStudio, then did a little editing. I had a dataset with ISO country codes added in already, and fortunately, the World Bank populates that automatically with data exports.
Country_Pop <- read.csv("~/R/win-library/3.0/Ref_Pop/Country_Pop.csv")
RefRes <- read.csv("~/R/win-library/3.0/Ref_Pop/RefRes.csv")
#Merge datasets on the ISO code variable
Mig_Total <- merge(Country_Pop,RefRes,by=c("Code","Code"))
#Remove any duplicate columns.
Mig_Total$Country.y <- NULL
#Write a new variable that calclated refugees as a percentage of each country's total population.
Mig_Total$PercentPop <- ((Mig_Total$Ref_Total/Mig_Total$Total)*100)
#Create a color palette, join the country data to rworldmap, and plot it.
colourPalette <- brewer.pal(7,'BuGn')
sPDF <- joinCountryData2Map(Mig_Total, joinCode = "ISO3", nameJoinColumn = "Code" )
mapCountryData(sPDF, nameColumnToPlot="PercentPop", mapTitle='Refugees as a Percent of
Total Host Population, 2012', colourPalette=colourPalette)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment