1
votes

I am trying to map California counties by using leaflet. I am not receiving any errors, but the map is not producing the correct results.

I found a .shp file for the county lines at https://data.ca.gov/dataset/ca-geographic-boundaries. I imported the .shp file using rgdal, and I attempted to map the boundaries by using the addPolygons() function.

`   # Importing CA County Lines
      ca_counties <- readOGR("Miller/CA_Counties/CA_Counties_TIGER2016.shp")
    # Mapping County Lines
      map1 <- leaflet() %>% 
              addTiles() %>% 
              addPolygons(data=ca_counties)
      map1`

Instead of county lines, the program is generating a singular line that is running through my entire map. I've attached a image of the output.

Failed Map

1

1 Answers

1
votes

When running your code I got the following warning:

Warning messages:
1: sf layer is not long-lat data 
2: sf layer has inconsistent datum (+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs).
Need '+proj=longlat +datum=WGS84' 

Projecting the polygon to WGS84 worked for me. I used the sf package because I find it a little more straight forward than rgdal.

library(sf)
library(leaflet)

ca_counties <- sf::read_sf('CA_Counties_TIGER2016.shp') %>%
  sf::st_transform('+proj=longlat +datum=WGS84')

map1 <- leaflet() %>% 
  addTiles() %>% 
  addPolygons(data=ca_counties)
map1