In this exercise, we outline how to gererate a “route map” - similar to one you might see published by an airline carrier. But instead of using flight routes, the map will show the “migration” of family members.
Initial Steps
We begin by loading the ggplot2 and ggrepel packages. The ggrepel package is loaded so that in case we wish to later add labels to the map, we can aesthetically do so.
library(ggplot2)
library(ggrepel)
We then load the data into r using the read.csv function.
migrate <- read.csv("addresses2.csv", stringsAsFactors = FALSE)
In order to construct the map, we will need longitude and latitude coordinates for all points which we wish to chart. The coordinates are already included in the migrate dataframe, so no further processessing is needed:
head(migrate,3)
## Name genLevel From.Address flat
## 1 Walter T Herget 2 335 Buena Vista Ave, Pekin, IL 61554 40.56595
## 2 James Herget 2 335 Buena Vista Ave, Pekin, IL 61554 40.56595
## 3 Robert L Herget 2 335 Buena Vista Ave, Pekin, IL 61554 40.56595
## flon To.Address tlat
## 1 -89.64698 2255 W Berry Ave, Littleton, CO 80120 39.61802
## 2 -89.64698 2700 Town Center Blvd N, Sugar Land, TX 77479 29.59531
## 3 -89.64698 438 S Gladstone, Aurora, IL 60506 41.75254
## tlon
## 1 -105.01363
## 2 -95.62172
## 3 -88.34685
Note that for each “route”, there must be 4 values: origin (1) longitude and (2) latitude coordinates, and destination (3)longitude and (4) latitude coordinates.
Generate Blank Map
The following command generates a blank map of the US that includes state borders:
#other map arguments include "world", "usa" and "county"
usMap <- borders("state", colour="grey", fill="white")
Generate US Migration Map Now we draw the map to include the migration routes.
ggplot() + usMap
allUSA <- ggplot() + usMap +
geom_curve(data=migrate,
aes(x=flon, y=flat, xend=tlon, yend=tlat),
col="#00008b",
size=.5,
curvature=0.2) +
geom_point(data=migrate,
aes(x=flon, y=flat),
colour="blue",
size=1.5) +
geom_point(data=migrate,
aes(x=tlon, y=tlat),
colour="blue") +
theme(axis.line=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
axis.ticks=element_blank(),
plot.title=element_text(hjust=0.5, size=12)) +
ggtitle("Migration Map: Three Generations of Adult Decendants of Walter F. Herget, Pekin, IL")
allUSA
Zooming In
Much of the famial migration is concentrated in the midwest, in particular, Illinois and Indiana. The following code “zooms-in” by setting x and y coordinate limits:
midwest <- ggplot() + usMap +
geom_curve(data=migrate,
aes(x=flon, y=flat, xend=tlon, yend=tlat),
col="#00008b",
size=.5,
curvature=0.2) +
geom_point(data=migrate,
aes(x=flon, y=flat),
colour="blue",
size=1.5) +
geom_point(data=migrate,
aes(x=tlon, y=tlat),
colour="blue") +
theme(axis.line=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
axis.ticks=element_blank(),
plot.title=element_text(hjust=0.5, size=12)) +
coord_cartesian(ylim=c(36.5, 42.5), xlim=c(-92, -82)) +
ggtitle("Migration Focus: Illinois, Indiana, Ohio")
midwest