rchoroplethchoroplethr

Is it possible to add an overlay of cities to a map made using choroplethr?


I am brand spankin' new when it comes to R. Any help you can give me will be greatly appreciated.

I am using the choroplethr package to create maps by ZIP code. I want to add an overlay showing cities (and only cities), but using reference_map = TRUE just returns a topographical map with state capitals in additional to making the actual colors harder to differentiate.

Is there a way to append a city overlay onto choroplethr, either with a built-in function of choroplethr that I am overlooking or by combining it with some other package?

My current function is

zip_choropleth(myexcel,
           title="Mansfield Geographical Capture by ZCTA",
           num_colors=9,
           state_zoom = c("massachusetts","rhode island","connecticut"), 
           reference_map = TRUE) 
           + scale_fill_brewer(palette="YlOrRd")

It returns an image like this:

Bad Topographical Map... D:

And I want something like this (plus the city overlay!):

Good Zip Map! :D


Solution

  • This is possible. There are a few things you need to understand in order to accomplish it:

    1. All choroplethr functions return ggplot2 objects. The scale for these plots is long and lat.
    2. You can add another layer to ggplots with the + operator.
    3. You need to know the long and lat of all the cities you want to add. According to google, Boston is at long=42.361145, lat=-71.057083.
    4. Choroplethr itself has no knowledge of cities and their locations.

    Here is demonstration of using geom_point to to add a black dot for Boston:

    library(choroplethrZip)
    library(ggplot2)
    
    data(df_pop_zip)
    
    zip_choropleth(df_pop_zip,
                   state_zoom = c("massachusetts","rhode island","connecticut")) +
      geom_point(aes(x=-71.057083, y=42.361145), size=5, color="black") 
    
    

    choropleth with dot for Boston

    Note: this just adds a dot. You would have to use something else to add the text. Take a look at the ggplot2 help files for ?geom_label and ?geom_text.