Throughout CPP 529, we have explored various ways to utilize Census Data, which inevitably led to learning about mapping in R. This inspired me to explore various types of graphs in R, introducing me to the Connection Map. This map can be a great tool to convey travel patterns between different cities on a scale ranging from global to the individual state levels. This can be a fun map to play around with, and there have also been successful data projects that have made use of it.
This Code-Through will walk through how to create a basic Connection Map, while also providing more advanced options to make your map unique. While there are many methods to uploading maps in R, the “Maps” and “Leaflet” packages are two of the most accessible for completing the task.
For this example, we will begin with drawing a map. The “Maps” package allows you to access a world map and USA maps. For this example, we will look at a USA map.
From there, we can choose four cities to convey having traveled to in the United States. For this example, let’s choose Portland, OR, Denver, CO, Nashville, TN, and New York, NY.
By doing a simple Google search, you can find the coordinates for these four cities. Using piping from the Dplyr package, you can graph all four points on the USA map.
# Cities
Portland <- c(-122, 45)
Denver <- c(-104, 39)
Nashville <- c(-86, 36)
NewYork <- c(-74, 40)
data <- rbind(Portland, Denver, Nashville, NewYork) %>%
as.data.frame()
colnames(data) <- c("longitude","latitude")
# Show the cities on the map
map('usa',
col="midnightblue", fill=TRUE
)
points(x=data$longitude, y=data$latitude, col="cornflowerblue", cex=2, pch=20)Once this is done, we should draw lines between the points to complete our Connection Map. Using the “Geosphere” package and the “gcIntermediate” function can assist with this. For each set of points you would like to connect, use the “gcIntermediate” function to calculate the distance between the two and draw a line in-between.
You can also add text to label each point by their corresponding city name.
map('usa',
col="midnightblue", fill=TRUE
)
points(x=data$longitude, y=data$latitude, col="cornflowerblue", cex=2, pch=20)
inter <- gcIntermediate(Portland, Denver, n=50, addStartEnd=TRUE, breakAtDateLine=F)
lines(inter, col="snow1", lwd=2)
inter <- gcIntermediate(Denver, Nashville, n=50, addStartEnd=TRUE, breakAtDateLine=F)
lines(inter, col="snow1", lwd=2)
inter <- gcIntermediate(Nashville, NewYork, n=50, addStartEnd=TRUE, breakAtDateLine=F)
lines(inter, col="snow1", lwd=2)
text(rownames(data), x=data$longitude, y=data$latitude, col="darkgoldenrod1", cex=1.5, pos=4)
This is a very simple example of a Connection Map, but the options and approaches are endless.
While the previous example provided a static image, we can take our Connection Map to the next level by making it more interactive. The “Leaflet” package allows us to do just that. The “Leaflet” package grants you access to upload specific “tiles”, or zoomable maps, to utilize in R.
Begin by adding your preferred tile. Here is an example:
Note that the code should begin and end with a lowercase “m”.
For this example, we will use one of the more popular tiles.
From there, we can add our cities using the “addMarkers”, or “addAwesomeMarkers” function.
Notice how the zoom automatically adjusts to the USA.
m <- leaflet() %>%
addTiles() %>%
addProviderTiles("NASAGIBS.ViirsEarthAtNight2012") %>%
addAwesomeMarkers(lng=data$longitude, lat=data$latitude)
mFinally, to complete our Connection Map, we need to connect our cities with lines. The “Leaflet” package allows you to connect them all in one grand swoop with the “addPolyLines” function.
m <- leaflet() %>%
addTiles() %>%
addProviderTiles("NASAGIBS.ViirsEarthAtNight2012") %>%
addAwesomeMarkers(lng=data$longitude, lat=data$latitude) %>%
addPolylines(lng = data$longitude, lat=data$latitude, weight=3, opacity=3, color="white")
mYou can also add labels to your markers with the “Leaflet” package. We can use this to add our corresponding city names to each point.
m <- leaflet() %>%
addTiles() %>%
addProviderTiles("NASAGIBS.ViirsEarthAtNight2012") %>%
addAwesomeMarkers(lng=data$longitude, lat=data$latitude) %>%
addPolylines(lng = data$longitude, lat=data$latitude, weight=3, opacity=3, color="white") %>%
addMarkers(lng = -122, lat = 45,
label = "Portland",
labelOptions = labelOptions(noHide = T)) %>%
addMarkers(lng = -104, lat = 39,
label = "Denver",
labelOptions = labelOptions(noHide = T)) %>%
addMarkers(lng = -86, lat = 36,
label = "Nashville",
labelOptions = labelOptions(noHide = T)) %>%
addMarkers(lng = -74, lat = 40,
label = "New York",
labelOptions = labelOptions(noHide = T))
mBoth the “Maps” and “Leaflet” packages are great resources for creating Connection Maps, but there are a multitude of ways that these mapping tools can be utilized to convey information!
This code through references and cites the following sources:
Connection Map. (2018). [https://www.r-graph-gallery.com/connection-map.html] (https://www.r-graph-gallery.com/connection-map.html)
Creating Interactive Spatial Maps in R Using Leaflet. (2019). [https://www.earthdatascience.org/courses/earth-analytics/get-data-using-apis/leaflet-r/] (https://www.earthdatascience.org/courses/earth-analytics/get-data-using-apis/leaflet-r/)
Leaflet Providers. (n.d.) [http://leaflet-extras.github.io/leaflet-providers/preview/index.html] (http://leaflet-extras.github.io/leaflet-providers/preview/index.html)