'Leaflet map 'Error in polygonData.default(data) : Don't know how to get path data from object of class data.frame'
I'm trying to create a leaflet map of census data.
I have a csv file contains the number of deaths across Ireland. The data consists of the year($Year), the Sex ($Sex), the location($NAME_1) and the age at death ($Age.at.Death)
Likewise I have a dbf file that contains the location ($NAME_1) and the geometry needed for polygons to create the map.
All of the data merges correctly into DEATHS1, but as soon as I pass this to leaflet I get the error 'Error in polygonData.default(data) : Don't know how to get path data from object of class data.frame'
library(leaflet)
library(sf)
library(dplyr)
DEATHS <- read.csv("VSA07.csv")
Shape <- read_sf("gadm36_IRL_1.dbf")
MergedData = inner_join(DEATHS,Shape) #inner joins by NAME_1 column, works okay.
DEATHS1 <- filter(MergedData, Year == "2007", Age.at.Death == "All ages", Sex == "Both sexes")
# Want a map satisfying the above conditions only for the moment, also works as intended
DEATHS1
bins <- c(0, 500, 1000, 2000, 3000, 4000, Inf)
pal <- colorBin("YlOrRd", domain = DEATHS1$VALUE, bins = bins)
m <-leaflet() %>%
addProviderTiles(providers$Stamen.TonerHybrid) %>%
addPolygons(data = DEATHS1)
m
The strange thing is that leaflet can plot a map based on MergedData but as soon as I apply any kind of filters through dplyr, ie DEATHS1, I get the error. So I don't think its anything to do with it being a data frame as MergedData is exactly that.
Any help is greatly appreciated.
A snapshot of the data DEATHS
ï..Statistic Year NAME_1 Sex Age.at.Death UNIT VALUE
1 Deaths Occurring 2007 State Both sexes Under 1 year Number 230
2 Deaths Occurring 2007 State Both sexes 1 - 4 years Number 54
Shape
A tibble: 26 x 11
GID_0 NAME_0 GID_1 NAME_1 VARNAME_1 NL_NAME_1 TYPE_1 ENGTYPE_1 CC_1 HASC_1 geometry
<chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <MULTIPOLYGON [°]>
1 IRL Ireland IRL.1~ Carlow Ceatharlach NA Administr~ County NA IE.CW (((-6.592422 52.7081, -6.592915 52.7073, -~
2 IRL Ireland IRL.2~ Cavan An Cabhán NA Administr~ County NA IE.CN (((-7.275174 53.78388, -7.275377 53.78377,~
3 IRL Ireland IRL.3~ Clare An Clár NA Administr~ County NA IE.CE (((-9.731943 52.64875, -9.731943 52.64903,~
DEATHS1
ï..Statistic Year NAME_1 Sex Age.at.Death UNIT VALUE GID_0 NAME_0 GID_1 VARNAME_1 NL_NAME_1 TYPE_1 ENGTYPE_1 CC_1 HASC_1 geometry
1 Deaths Occurring 2007 Wicklow Both sexes All ages Number 819 IRL Ireland IRL.26_1 Administrative County County <NA> IE.WW MULTIPOLYGON (((-6.109166 5...
Any help is much appreciated/
Solution 1:[1]
Issue is that when merging an sf file (the .dbf file) with the csv file data, R secretly converts the output to a tibble, thus leaflet loses the ability to identify the 'geometry' column without assistance.
To fix it you must explicitly tell leaflet where the geometry column is as shown below %>% addPolygons(data = MergedDataFilter$geometry)
m
m <-leaflet() %>%
addProviderTiles(providers$Stamen.TonerHybrid) %>%
setView(lng = -7.4653, lat = 53.5345, zoom = 6.95) %>%
addPolygons(data = DEATHS1$geometry,
weight = 1,
color = "#660000",
fillColor = pal(DEATHS$VALUE))
#m
Solution 2:[2]
You need to convert the data.frame back to an object with sf
class.
library(sf)
DEATHS1 <- sf::st_as_sf(DEATHS1)
m <-leaflet() %>%
addProviderTiles(providers$Stamen.TonerHybrid) %>%
addPolygons(data = DEATHS1)
m
It should work.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | user12114019 |
Solution 2 | Araê Souza |