Creating US Immigration Path Map in Tableau with R

There are loads of videos and articles teaching you how to create a destination map from a dataset containing all the information you need, for example, this video uses delayed flights data wherein each row, origin and destination are already provided..What if your dataset is not a standard flight dataset like this?.Here, I will present you an example using immigration data from Department of Homeland Security (the first supplementary table in the attachment)..The data looks like this:The leftmost column is immigrants’ home country and the headers are the states they are moving to..If we are going to produce a destination map, the data should follow a pattern like this according to Tableau tutorial:from Tableau Online helpThe last three columns are required..The path ID is used to identify the origin and destination’s location(longitude & latitude) data.Data PreparationSince we want to illustrate the path in 10 years and there are 10 excel files, it’s best you create a data processing function first and apply it to all the files..We first start by reading and cleaning the data.1..Data readingyear = 2017path = paste(as.character(year), ".xlsx", sep="")df = read_excel(path, na = c("D", "-"), skip = 3)df = df[-c(1:11)..-2]df = df[ , !(names(df) %in% c("U.S. Armed Services Posts", "U.S. Territories1","Guam", "Unknown"))]df = head(df,-5)colnames(df)[1] = 'country'I strip off some unnecessary data and notes in the spreadsheet along with some columns that actually not state (e.g. Armed Services)..In addition, some cells are “D” — data withheld to limit disclosure..I treat them as NA.2..Data CleaningThe table only records the birth country of immigrants.. More details

Leave a Reply