Leaflet addPolylines不绘制数据对象的所有值(或行)

时间:2016-12-19 21:41:23

标签: r widget leaflet contour maptools

我使用addPolylines覆盖leaflet生成的网络地图上特定值的轮廓,但addPolylines并未绘制所有数据。随附。根据第一幅图像, 125 的至少两个区域是明显的,但是小叶生成的地图仅显示一个区域。
可以从here下载数据,并为addPolylines生成SpatialLinesDataFrame对象并生成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)

Precipitation patterns

## 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

WebMap with one Contour Line

## 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])

Webmap with two contour lines

目前尚不清楚为什么addPolylines没有绘制最初提供的所有坐标。非常感谢建议。

1 个答案:

答案 0 :(得分:1)

通过devtools::install_github("rstudio/leaflet")从github更新到最新的传单版本可以解决您的问题。这是我的sessionInfo(),您的代码运行得很好,我看到3行:

R version 3.3.2 (2016-10-31)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 14.04.5 LTS

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=de_DE.UTF-8       
 [4] LC_COLLATE=en_US.UTF-8     LC_MONETARY=de_DE.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=de_DE.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=de_DE.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] maptools_0.8-39    sp_1.2-3           leaflet_1.0.2.9010

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.8     magrittr_1.5    devtools_1.12.0 xtable_1.8-2    lattice_0.20-34 R6_2.2.0       
 [7] httr_1.2.1      tools_3.3.2     grid_3.3.2      git2r_0.15.0    withr_1.0.2     htmltools_0.3.5
[13] crosstalk_1.0.0 yaml_2.1.14     digest_0.6.10   shiny_0.14.2    htmlwidgets_0.8 curl_2.1       
[19] memoise_1.0.0   mime_0.5        jsonlite_1.1    httpuv_1.3.3    foreign_0.8-67