我想使用leafletR::leaflet
创建一个等值区域地图。
我的数据来自SpatialPolygonsDataFrame
,我想选择要绘制的特定列。
使用sp::spplot
,这很容易,因为参数zcol
允许我指定要绘制的图层/列:
library("maptools");library("sp");library("leafletR")
SP <- readShapePoly(system.file("shapes/sids.shp",
package="maptools")[1],
proj4string=CRS("+proj=longlat +datum=WGS84
+no_defs +ellps=WGS84
+towgs84=0,0,0"))
spplot(SP, zcol="BIR79")
但是,对于leafletR
,我不知道如何指定图层,它只是绘制了普通地图边框:
SP4leaflet <- toGeoJSON(data=SP, dest=tempdir(), name="BIR79")
SPleaflet <- leaflet(data=SP4leaflet, dest=tempdir(),
title="Trying to plot BIR79",
base.map="osm", popup="*")
SPleaflet
有关如何使用leafletR
选择所需图层/列的任何想法?
答案 0 :(得分:2)
为了完整起见,我想提一下,现在可以使用library(mapview)
轻松实现。
library("maptools")
library("sp")
library("mapview")
SP <- readShapePoly(system.file("shapes/sids.shp",
package="maptools")[1],
proj4string=CRS("+proj=longlat +datum=WGS84
+no_defs +ellps=WGS84
+towgs84=0,0,0"))
spplot(SP, zcol="BIR79")
mapview(SP, zcol="BIR79")
有关类似示例,另请参阅http://edzer.github.io/sp/#interactive-maps-leaflet-mapview,mapview
了解http://127.0.0.1:8000/stream.mp3
的更多功能。{/ p>
答案 1 :(得分:1)
也许这样的事情有效。请注意,我使用leaflet
而不是leafletR
。这里有一个很好的介绍:here
library(leaflet)
library(maptools)
library(sp)
library(classInt)
library(classInt)
SP <- readShapePoly(system.file("shapes/sids.shp",
package="maptools")[1],
proj4string=CRS("+proj=longlat +datum=WGS84
+no_defs +ellps=WGS84
+towgs84=0,0,0"))
spplot(SP, zcol="BIR79")
## 6 classes with fixed given breaks
nclass <- classIntervals(SP$BIR79, n=6)
## Color for each class
colcode = findColours(nclass, c("red", "blue"))
leaflet(SP) %>% addTiles() %>% addPolygons(data = SP, color = colcode)
答案 2 :(得分:1)
您的代码缺少样式定义。对于渐变样式,您必须使用styleGrad
创建样式对象。 prop
中的styleGrad
参数指定了您想要显示的属性。
library("maptools")
library("sp")
library("leafletR")
SP <- readShapePoly(system.file("shapes/sids.shp",
package="maptools")[1],
proj4string=CRS("+proj=longlat +datum=WGS84
+no_defs +ellps=WGS84
+towgs84=0,0,0"))
SP4leaflet <- toGeoJSON(data=SP, dest=tempdir(), name="BIR79")
SPleaflet <- leaflet(data=SP4leaflet, dest=tempdir(),
title="Trying to plot BIR79",
base.map="osm", popup="*")
## missing style definition
brks <- seq(0, max(SP$BIR79), by=5000)
clrs <- colorRampPalette(c("blue","yellow", "red"))(7)
stl <- styleGrad(prop="BIR79", breaks=brks, style.val=clrs,
out=1, leg="BIR79")
## end style definition
SPleaflet <- leaflet(data=SP4leaflet, dest=tempdir(),
title="Trying to plot BIR79", base.map="osm",
style=stl, popup="*")
SPleaflet