2

I've just started using the leaflet package in R to create interactive maps and I'm wondering if it's possible to plot one of the leaflet providers on top of a plotted polygon rather than under it as a basemap. Here's some example code:

library(raster)
library(leaflet)

ph <- getData("GADM", country = "PHL", level = 1)

leaflet(data = ph) %>%
  addProviderTiles("OpenWeatherMap.Precipitation") %>% 
  addPolygons(fillColor = "red", 
              fillOpacity = 1, 
              color = "#FFFFFF", 
              weight = 1.3)

This simply plots a shapefile of the Philippines (in my real example, it's a choropleth map). The precipitation basemap was found here (toward the bottom of the list; OpenWeatherMap.Precipitation). Here's what the output looks like:

enter image description here

It might be a little difficult to see, but the precip layer is plotted as a basemap, so all of that info is going under the ph shapefile. I'm trying to find a way so that the precipitation layer goes OVER the Philippines shapefile. I'm not sure if this is even possible, but it seems like I should be able to have a plain OSM basemap with the shapefile plotted on top and the precipitation layer on top of the .shp. As it is now, I know that I can alter the opacity of the shapefile, but this isn't much of a solution for a detailed choropleth map. Is this possible, and if so, how do I do it?

  • Though I don't know what the equivalent R code would be, here's an example (idea adapted from this Block) of how to bring a tile layer to the top in leaflet.js. It uses the tileLayer.setZIndex method as well as the internal map._createPane method, which might not be readily accessible in Leaflet for R. – nathansnider Feb 24 '16 at 20:46
2

This has now been answered here https://stackoverflow.com/questions/43881007/how-to-addtiles-on-top-of-addpolygons-in-rs-leaflet, with addMapPane function.

1

As a temporary solution, you could simply do the following

library(mapview)
m <- mapview(ph, map.types = "OpenWeatherMap.Precipitation")
m

and manually deselect the ph polygons from the list of layers in the top-left corner.

map

0

There is another way to do this, but with data from WorldClim. This is the code I used to get the desired result:

library(rgdal)
library(RCurl)
library(httr)
library(jsonlite)
library(leaflet)
library(mapview)
library(owmr)
library(raster)
library(RColorBrewer)
setwd("D:/Works/Fernando/CRS/R/shapes/")
getwd()
file.exists(".","contorno.shp")
ph <- raster::getData('worldclim',var='tmin',res=0.5, lon=-86.921641,lat=14.761664)
pais<-rgdal::readOGR(dsn=getwd(),layer="contorno")
zona<-rgdal::readOGR(dsn=getwd(),layer="La_Paz_Valle")
#newproj <- "+proj=longlat +datum=WGS84"
crs(ph$tmin1_23)
crs(pais)
prj<-"+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"
pais2<-spTransform(pais,crs(prj))
zona2<-spTransform(zona,crs(prj))
crs(pais2)
cp<-crop(ph$tmin1_23,pais2)
s<-stack(ph$tmin1_23)
coors<-read.csv("sequia.csv")
coords<-data.frame(lon=coors$LONGITUDE, lat=coors$LATITUDE)
coordinates(coords)<-c("lon","lat")
val<-extract(x=ph$tmin1_23, y=coords)
#proj4string(xy) <- CRS(prj)  ## for example
#sfc<-st_sfc(xy,prj)
#xy<-data.frame(x=coors$LONGITUDE,Y=coors$LATITUDE)
#require(sf)
#res<-SpatialPoints(cbind(coors$LONGITUDE, -coors$LATITUDE), proj4string=CRS("+proj=longlat"))
#as(res,"SpatialPoints")
#rasValue<-lapply(ph$tmin1_23,extract, res)
colores <- c('red', 'green', 'blue', 'chocolate', 'deeppink', 'grey')
at <- seq(0, 4800, 800)
cb <- colorBin(palette = colores, bins = at, domain = at)
m<-leaflet() %>%
  addTiles()%>%
  addRasterImage(cp,opacity = 0.8)%>%
  addLegend(pal = cb, values = at)%>%
  addPolygons(data = zona2,fillColor = "#ff0000",opacity = 0.1)
m<-addMarkers(m,lng=coors$LONGITUDE,lat=coors$LATITUDE,popup=paste("Region:",coors$Municipality,"<br>",
                                                                   "Precipitacion:","<br>",
                                                                   val,"mm/mes"))
#m<-addMarkers(m,lng=-87.8333,lat = 14.1167,popup=paste("Guajiquiro","prec:",val,sep = " "))
#m<-addMarkers(m,lng = -87.7,lat = 13.9667,popup="San Juan")
#m<-addMarkers(m,lng = -87.95,lat = 14.0667,popup = "Santa Ana")
#m<-addMarkers(m,lng = -87.5333,lat = 13.65,popup = "San Francisco de Coray")
#m<-addMarkers(m,lng = -87.6525,lat = 13.62083,popup = "Langue")
#m<-addMarkers(m,lng = -87.7167,lat = 13.7333,popup = "Aramecina")
#m %>% mapview(ph, map.types = "OpenWeatherMap.Precipitation")
m

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.