0

I am attempting to plot profitability on top of counties in Minnesota, Iowa, and Nebraska. Using leaflet and tigris, I have been able to plot ALL counties, whether or not I have data for it. This leaves me with a few counties with colors and the rest labeled as NA. Is there a way for me to remove all NA's from my geo_join data so that it just isn't used ala unused Wisconsin areas? I have tried using fortify, but I can't figure out how to determine what county boundaries I'm looking at when I merge the TIGER boundary lines with my County FIPS file in order to remove them.

Here is what my leaflet currently looks like: enter image description here

My code to get the map is this:

library(tigris)
library(leaflet)

pal <- colorNumeric(c("yellow","dark red"),county$Construction.Cost,na.color="white")
IA_counties <- counties(state="IA", cb=TRUE, resolution ="20m")
MN_counties <- counties(state="MN",cb=TRUE,resolution="20m")
NE_counties <- counties(state="NE",cb=TRUE,resolution="20m")
IA_merged <- geo_join(IA_counties,county,"GEOID", "GEOID")
MN_merged <- geo_join(MN_counties,county,"GEOID","GEOID")
NE_merged <- geo_join(NE_counties,county,"GEOID","GEOID")
popupIA <- paste0("County Projects: ", as.character(paste('$',formatC(format(round(IA_merged$Construction.Cost, 0), big.mark=',', format = 'f')))))
popupMN <- paste0("County Projects: ", as.character(paste('$',formatC(format(round(MN_merged$Construction.Cost, 0), big.mark=',', format = 'f')))))
popupNE <- paste0("County Projects: ", as.character(paste('$',formatC(format(round(NE_merged$Construction.Cost, 0), big.mark=',', format = 'f')))))


leaflet() %>%
      addProviderTiles("MapQuestOpen.OSM") %>%
      addLegend(pal = pal, 
                values = IA_merged$Construction.Cost, 
                position = "bottomright", 
                title = "County Projects",
                labFormat=labelFormat(prefix="$")) %>%
      addCircles(lng=yup2$lon, lat=yup2$lat,weight=.75,fillOpacity=0.01,color="red",
                 radius = 96560) %>%
      addCircles(lng=yup2$lon, lat=yup2$lat,weight=.75,fillOpacity=0.01,color="blue",
                 radius = 193121) %>%
      addPolygons(data = IA_counties, 
                  fillColor = ~pal(IA_merged$Construction.Cost), 
                  layerId=1,
                  fillOpacity = .25, 
                  weight = 0.05, 
                  popup = popupIA)%>%
      addPolygons(data=MN_counties,
                  fillColor=~pal(MN_merged$Construction.Cost),
                  fillOpacity=0.25,
                  weight=0.05, 
                  popup = popupMN) %>%
      addPolygons(data=NE_counties,
                  fillColor=~pal(NE_merged$Construction.Cost),
                  fillOpacity=0.25,
                  weight=0.05, 
                  popup = popupNE) 

I apologize for not including reproducible data, but if needed, please ask. I'm hoping that this is more of a simple na.color= formula solution. The map looks "okay" as of now, but I'd like it if it's possible to not have to make the fillOpacity so light so the NA counties don't stand out.

Thanks for any and all help and please, let me know if you have any questions!

medavis6
  • 843
  • 10
  • 32

2 Answers2

2

I'm the creator of the tigris package. Thanks so much for using it! In the development version of tigris on GitHub (https://github.com/walkerke/tigris), I've added an option to geo_join to accommodate inner joins, which would remove the unmatched data entirely from the resultant spatial data frame (if this is what you are looking for). You can also supply a common merge column name as a named argument to the new by parameter if you want. For example:

IA_merged <- geo_join(IA_counties, county, by = "GEOID", how = "inner")

should work. I'm still testing but I'll probably submit this update to CRAN in January.

kwalkertcu
  • 1,011
  • 6
  • 8
  • Thanks for the heads up! Just want to say that I really appreciate your `tigris` package and its usefulness with `leaflet`. Keep up the great work! – medavis6 Dec 15 '15 at 17:36
0

So, embarrassingly, the answer to this question was as simple as I had hoped. I tweaked the following na.color code and it worked exactly as I wanted.

pal <- colorNumeric(c("yellow","dark red"),county$Construction.Cost,na.color="transparent")

enter image description here

medavis6
  • 843
  • 10
  • 32