我正在尝试使用Leaflet包在R中绘制GeoJSON。下面是代码和错误。
library(geojsonio)
library(leaflet)
library(data.table)
library(plyr)
library(rgdal)
library(sp)
library(RColorBrewer)
library(rgeos) #for simplification
library(leafletR)
library(sf)
library(jsonlite)
library(RJSONIO)
mydata <- fromJSON("https://gist.githubusercontent.com/senthilthyagarajan/eb7a2771eab4639e94d5f9eaad28cb33/raw/1cfe355a56d2c1856a70a5389a4eadf06d782748/data.geojson",flatten=TRUE)
leaflet(mydata) %>%
addPolygons(color = "#444444", weight = 1, smoothFactor = 0.5,
opacity = 1.0, fillOpacity = 0.5,
fillColor = ~colorQuantile("YlOrRd", nghbrhd)(nghbrhd),
highlightOptions = highlightOptions(color = "white", weight = 2,
bringToFront = TRUE))
Error: lexical error: invalid char in json text.
FeatureCollection
(right here) ------^
请忽略上面提到的一长串软件包。
答案 0 :(得分:1)
您的数据必须是&#34; SpatialPolygonsDataFrame&#34;类型。当我运行上面的代码时,mydata是类型列表,它产生错误。我使用geojsonio包中的geojson_read来读取数据(指定sp:spatialpolygons数据类型),我得到了一个传单图。 Flatten不是geojson_read函数中的参数,但是如果感兴趣,可以查看parse参数以在数据框中转换geojson对象。
library(leaflet)
mydata <- geojsonio::geojson_read("https://gist.githubusercontent.com/senthilthyagarajan/eb7a2771eab4639e94d5f9eaad28cb33/raw/1cfe355a56d2c1856a70a5389a4eadf06d782748/data.geojson",what = "sp")
leaflet(mydata) %>%
addPolygons(color = "#444444", weight = 1, smoothFactor = 0.5,
opacity = 1.0, fillOpacity = 0.5,
fillColor = ~colorQuantile("YlOrRd", nghbrhd)(nghbrhd),
highlightOptions = highlightOptions(color = "white", weight = 2,
bringToFront = TRUE))