Tidy Tuesday Revisited: Interactive Map of Arlington Historic Neighborhoods

[This article was first published on Louise E. Sinks, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

This week’s TidyTuesday is about place names as recorded by the US Board on Geographic Names. The dataset has been cleaned to include only populated places. I ended up augmenting the dataset with information about Arlington Historic neighborhoods and current neighborhood boundaries. My post with code on this project is here.

I wanted to create an interactive map with leaflet, but I encountered two problems:

1- I couldn’t figure out how to add my civic association map.

2- The map that I did make worked fine when I ran it from a code chunk, but failed when I rendered the quarto document.

I’ve solved both problems and I really enjoyed working with leaflet.

Here are the libraries:

library(tidyverse) # who doesn't want to be tidy?
library(sf) # for handling geo data
library(leaflet) # interacting mapping

I saved the two datasets from my previous work: historic_4269 and arlington_polygons_sf. I saved them using:

st_write(historic_4269, "points.shp")

st_write(arlington_polygons_sf, "polygons.shp")

from the sf package.

Here, I’m reading them in. The process does change some of the variable names. The dataset from the National Register of Historic Places had non-standard names such as Property.Name, which gets converted to a shorter name, Prprt_N, with _ instead of period.

historic_4269 <- st_read("points.shp")
arlington_polygons_sf <- st_read("polygons.shp")

I mentioned that I found tutorials here and here to make the pop-up URL using leaflet. So, following them I add the HTML anchor tag.

# turn the url to HTML anchor tag
historic_4269 <- historic_4269 %>% 
  mutate(tag = paste0("More Info: <a href=", Extrn_L,">", Extrn_L, "</a>"))

Leaflet uses background map tiles as the canvas for the map. As with all mapping, the coordinate reference system (CRS) of all your component layers needs to be the same. The two datasets I have used the CRS= 4269 projection, but this isn’t the usual CRS. The background map I chose uses the 4326 CRS, so I need to transform my data to that projection. Leaflet will give you a warning if you add layers with unexpected CRSs, so make sure to read the messages carefully and correct them.

historic_4326 <- sf::st_transform(historic_4269, crs = 4326)
arlington_polygons_sf_4326 <- sf::st_transform(arlington_polygons_sf, crs = 4326) 

For the issue of adding the polygon data, I was just not really thinking about things. Leaflet uses tidyverse piping, so you either need to have the dataset at the start of the pipe chain or you need to explicitly pass it as data = blah. The error message wasn’t super help to me either : addPolygons must be called with both lng and lat, or with neither. I thought that meant I needed to transform the polygons into some other type geometry format.

So this doesn’t work:

leaflet_map <- leaflet() %>%



But this does:

leaflet_map <- leaflet(arlington_polygons_sf_4326) %>% 

To leave a comment for the author, please follow the link and comment on their blog: Louise E. Sinks.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)