I am trying to plot a raster over New Zealand in leaflet but I am having some issues. I've found that polygons and points/markers plot as expected:
library(raster)
library(leaflet)
library(dplyr)
library(sp)
p1 <- SpatialPolygons(list(Polygons(list(Polygon(cbind(x = c(165, 165, 185, 185, 165), y = c(-50, -31, -31, -50, -50)))), 1)), proj4string=CRS("+init=epsg:4326"))
p2 <- data.frame(lng = runif(100, 165, 185), lat = runif(100, -50, -31))
leaflet() %>%
addTiles() %>%
addPolygons(data = p1) %>%
addMarkers(lat = p2$lat, lng = p2$lng)
And a raster with a maximum longitude of 180 will plot just fine:
r <- raster(xmn = 165, xmx = 180, ymn = -50, ymx = -31, crs = "+init=epsg:4326", nrows = 50, ncols = 50)
r[] <- rnorm(ncell(r))
leaflet() %>% addTiles() %>% addRasterImage(r, opacity = 0.5)
But a raster with a longitude extending beyond 180 that covers all of New Zealand does not plot properly:
r <- raster(xmn = 165, xmx = 185, ymn = -50, ymx = -31, crs = "+init=epsg:4326", nrows = 50, ncols = 50)
r[] <- rnorm(ncell(r))
leaflet() %>% addTiles() %>% addRasterImage(r, opacity = 0.5)
And while I can rotate the raster and get it to plot, it splits the raster and plots the part beyond 180 back at 0:
r <- raster(xmn = 165, xmx = 185, ymn = -50, ymx = -31, crs = "+init=epsg:4326", nrows = 50, ncols = 50)
r[] <- rnorm(ncell(r))
leaflet() %>% addTiles() %>% addRasterImage(rotate(r), opacity = 0.5)
I'm wondering if anyone else has run into this issue and if so how to get around it?