Leaflet addPolylines is not plotting all values (or lines) of a data object

1.4k views Asked by At

I'm using addPolylines to overlay contours of a specific value(s) on a leaflet generated web map, however, addPolylines is not plotting all the data that it is supplied with. As per the first image, at least two regions of a value of 125 are evident, but leaflet generated map displays only one region.
The data can be downloaded from here, and reproducible script that generates SpatialLinesDataFrame object for addPolylines and leaflet generated map as follows:

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 has co-ordinates for three lines, but, addPolylines plots contours only for one region

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

It is not clear why addPolylines is not plotting all the coordinates that it supplied with initially. Greatly appreciate suggestions.

1

There are 1 answers

0
TimSalabim On BEST ANSWER

Updating to the latest leaflet version from github via devtools::install_github("rstudio/leaflet") should solve your problem. Here's my sessionInfo() with which your code runs just fine and I see 3 lines:

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