I am trying to plot the site of some disease-events data on a map.
I use this to import the data:
ByTown<-readOGR(dsn="C:/temp/lyme/Towns", layer="Towns", encoding = "UTF-8", verbose= FALSE)
check the class:
class(ByTown)
#getting this result
[1] "SpatialPolygonsDataFrame"
attr(,"package")
[1] "sp"
Then I convert all of the factors to character data and check to see that I still have a SpatialPolygonsDataFrame
using class
again, which I do:
Then I format the data I wish to merge into the same title case as the original:
townCount$City<-str_to_title(townCount$City)
Then I geo_join the count data to the spatial polygon data frame:
ByTown<-geo_join(ByTown, townCount,"MCD_NAME", "City")
Then I set the palette and run the mapping:
pal = colorQuantile("PuOr",ByTown$count, n=5 )
map<-leaflet(ByTown) %>%
addProviderTiles("CartoDB.Positron")%>%
addPolygons(fillColor = ~pal(count),
color = "#000000",
stroke = TRUE,
weight = 1,
smoothFactor = 0.5,
options(viewer = NULL))
map
And I get this error:
Error in derivePolygons(data, lng, lat, missing(lng), missing(lat), "addPolygons") :
addPolygons must be called with both lng and lat, or with neither.
I have looked in the coordinates slots and there is data in there...I am baffled by the error and not finding any useful answers online. Here is the head of the first polygon in coordinates slot:
head(nByTown@polygons[[1]]@Polygons[[1]]@coords )
[,1] [,2]
[1,] 1036519 916318.7
[2,] 1036039 916355.8
[3,] 1031757 916299.7
[4,] 1027474 916244.5
[5,] 1026709 916198.1
[6,] 1026826 916248.3
Any one every have this issue, identify the root cause and fix it?