I'm stumped getting my dataframe to plot in leaflet. I have one shapefile and one csv that I merged together. The resulting dataframe has several columns, including long, lat, and "percent".
I am able to plot this using ggplot with the following code:
p <- ggplot() +
geom_polygon(data = nyc_plotData, aes(x=long, y=lat, group = group,
fill=percent)) +
geom_polygon(data = county, aes(x=long, y=lat, group = group),
fill=NA, color = "black", size = 0.25) +
coord_map(xlim = c(-74.26, -73.71), ylim = c(40.49,40.92))
The result is a choropleth map of income distribution in nyc:
When I try to use the same dataframe in leaflet, I get this error:
Don't know how to get path data from object of class data.frame
I understand I have to reformat my dataframe. I tried various ways to convert to a SpatialPolygonDataFrame. For example:
xy <- nyc_plotData[,c(1,2)]
spdf <- SpatialPolygonsDataFrame(coords = xy, data = nyc_plotData,
proj4string = CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"))
gives the following error:
Error in SpatialPolygonsDataFrame(coords = xy, data = nyc_plotData, proj4string = CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")) : unused arguments (coords = xy, proj4string = CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"))
I'm clearly missing something, but I haven't been able to find any examples of this problem anywhere online.
I'd really appreciate any tips or advice dealing with shapefiles and plotting in leaflet.