【发布时间】:2016-12-20 22:54:25
【问题描述】:
我正在使用addPolylines 在leaflet 生成的网络地图上覆盖特定值的等高线,但是,addPolylines 并未绘制它提供的所有数据。根据第一张图像,至少有两个值为 125 的区域是明显的,但传单生成的地图仅显示一个区域。
数据可以从here下载,生成SpatialLinesDataFrame对象的可重现脚本为addPolylines和leaflet生成地图如下:
rm(list=ls())
library(leaflet)
library(maptools)
# create a vector of lon, lat, and read data
lon1 <- seq(-124.938,length=59,by=1)
lat1 <- seq(25.063,length=29,by=1)
data1 <- matrix(scan(file="AnnualPrcpValues.txt"),ncol=length(lat1),byrow = T)
## spatial pattern of precipitation
## I choose a value *125* correspond to light yellow color to plot contours on leaflet generated map
## at least two regions corresponds to this value, i.e., Pacific northwest & Southwest region]
filled.contour(lon1,lat1,data1,color=terrain.colors)
## The following code calculates contour lines correspond to a value *125*
## and then converts contour lines into a SpatialLinesDataFrame object
CL=contourLines(lon1,lat1,data1,levels=c(125))
c.linesSP <- ContourLines2SLDF(CL)
c.linesSP 具有三条线的坐标,但是,addPolylines 仅绘制一个区域的等高线
str(coordinates(c.linesSP))
##List of 1
## $ :List of 3
## ..$ : num [1:17, 1:2] -123 -122 -122 -122 -122 ...
## ..$ : num [1:5, 1:2] -125 -124 -123 -124 -125 ...
## ..$ : num [1:9, 1:2] -86.4 -86.9 -87.9 -88.9 -89.9 ...
## leaflet generated map
map1 <- leaflet() %>% addTiles() %>%
setView(lng = -80, lat = 40, zoom = 2)
map2<- map1 %>% addPolylines(data=c.linesSP,color="red")
## It should have three lines, but only one line is seen in the Pacific Northwest region
map2
## However, contour line in the southwest region is plotted when explicilty co-ordinates, i.e., [[1]] [[3]] are supplied
##
tempdata <- coordinates(c.linesSP)[[1]][[3]]
map2 %>% addPolylines(tempdata[,1],tempdata[,2],
weight=1.25,color="green") %>%
addMarkers(tempdata[,1],tempdata[,2])
不清楚为什么addPolylines 没有绘制它最初提供的所有坐标。非常感谢您的建议。
【问题讨论】:
-
检查任何缺失/不适用的数据 - 请参阅 here for example
-
实际数据有缺失值,但是,
c.linesSP[SpatialLinesDataFrame 对象,提供给 addPolylines] 没有任何缺失值。c.linesSP有四个槽,即数据、行(本质上是坐标)、b.box 和 proj4string。 -
@SymbolixAU:当显式传递坐标时,传单命令工作得很好,因此,它可能与丢失/NA 数据无关 (??)
标签: r widget leaflet contour maptools