0
votes

I am trying to create a map showing generation capacity by county with leaflet. There can be multiple generating units per county, but I thought that would not be an issue for mapping county capacity. I pulled county geometries from tidycensus, and joined those with the dataset for county generation capacity. I have included my code below. Any help/insight would be much appreciated!

  Texas_counties <- get_acs(state = "TX", geography = "county", 
                      variables = c(totalpop = "B01003_001"), geometry = TRUE)

  Texas_counties <- Texas_counties %>%
  mutate(NAME = gsub(" County, Texas", "", NAME))

  Texas_counties <- Texas_counties %>%
    mutate(NAME = toupper(NAME))
  Texas_counties$GEOID <- NULL
  Texas_counties$variable <- NULL
  Texas_counties$estimate <- NULL
  Texas_counties$moe <- NULL

  colnames(Texas_counties)[1] <- "COUNTY"

  ERCOT_counties <- left_join(ERCOT_report, Texas_counties, by = "COUNTY")

  pal <- colorNumeric("viridis", NULL)

 leaflet(ERCOT_counties) %>%
 addTiles() %>%
 addPolygons(data = geometry, stroke = FALSE, smoothFactor = 0.3, 
 fillOpacity = 1,
           fillColor = ~pal(COUNTY_CAPACITY))

Error in polygonData.default(data) : Don't know how to get path data from object of class standardGeneric

1
You are passing the parameter data = geometry to addPolygons. Where is geometry coming from? You don't seem to have that defined anywhere. Maybe you meant data = ~geometry?MrFlick
Geometry is coming from Texas_counties. It is pulled in as a part of the get_acs function where geometry = TRUEbgcorbett95
Then you need to use the ~ before the name to let leaflet know the values are coming from the data objectMrFlick
I do that and then I get "Don't know how to get path data from object of class formula"...when I run str(ERCOT_counties), it returns geometry as a sfc_Multipolygonbgcorbett95

1 Answers

1
votes

Since we don't have the contents of your ERGOT_report data frame, it's hard to know, but my guess is that you're getting that error because you're using a simple data frame and not an sf or spatial data frame in your leaflet() call.

Whenever possible, I find it easiest to have my data and spatial features together in one sf object. So here is an example of how you would get the county geometry from tigris, join it with a non-spatial data frame (I just made up some data here) and then make a map. Note when you are joining a spatial data frame and a non-spatial data frame, the order of the join matters. So if you want to output of your join to be spatial, the first data frame must be the sf object and the second is the non-spatial data frame.

library(tidyverse)
library(leaflet)
library(tigris)
library(sf)

# get county geometries using tigris
tx_counties <- counties(state = "TX", cb = TRUE) %>% 
  transmute(COUNTY = toupper(NAME))

# create some made up data for 50 of the tx counties
ercot_report <- tibble(
  COUNTY = tx_counties$COUNTY,
  COUNTY_CAPACITY = rpois(nrow(tx_counties), lambda = 10)
  ) %>% 
  slice_sample(n = 50)

# join geometry to data
ercot_counties <- tx_counties %>% 
  inner_join(ercot_report) %>% 
  st_transform(4326)

# create palette to use in leaflet map
pal <- colorNumeric(
  palette = "viridis",
  domain = ercot_counties$COUNTY_CAPACITY
  )

# map!
leaflet(ercot_counties) %>%
  addTiles() %>%
  addPolygons(
    stroke = FALSE,
    smoothFactor = 0.3, 
    fillOpacity = 1,
    fillColor = ~pal(COUNTY_CAPACITY)
    )

texas county leaflet map