(CC-BY-NC-SA)

This document was produced on So Jun 12 2016 using mapview version 1.1.0


mapview provides a few convenience functions for popup creation. These popups can be used with both mapview and leaflet.


popupTable

This is the standard popup function used by mapview

library(mapview)
mapview(breweries91, zcol = "founded")


It also works with leaflet and can be controlled to only show selected columns of the attribute table

leaflet() %>% 
  addTiles() %>% 
  addCircleMarkers(data = breweries91, popup = popupTable(breweries91, zcol = c("brewery", "village", "founded")))

popupGraph

To include lattice or ggplot2 or htmlwidgets based graphs in our popups we can use ppopupGraph(). We have three options to embed grpahs in popups:

  1. type = "png" for png rendering of graphs
  2. type = "svg" for svg rendering of graphs
  3. type = "html" for html rendering of graphs (use this option to render htmlwidgets based visualisations)
library(sp)
library(lattice)

pnt <- data.frame(x = 174.764474, y = -36.877245)

coordinates(pnt) <- ~ x + y
proj4string(pnt) <- "+init=epsg:4326"

p2 <- levelplot(t(volcano), col.regions = terrain.colors(100))

mapview(pnt, popup = popupGraph(p2, width = 300, height = 300))


data(meuse)
coordinates(meuse) <- ~ x + y
proj4string(meuse) <- CRS("+init=epsg:28992")

## create plots with points colored according to feature id
p <- xyplot(copper ~ cadmium, data = meuse@data, col = "grey")
p <- mget(rep("p", length(meuse)))

clr <- rep("grey", length(meuse))
p <- lapply(1:length(p), function(i) {
  clr[i] <- "red"
  update(p[[i]], col = clr)
})

mapview(meuse, popup = popupGraph(p, type = "svg", width = 3, height = 2.5))


Let’s have a look at the poor state of the Aral Sea by providing a slideview image comparison of the lake between 2000 and 2013 in a popup bound to a point in the lake (or better what used to be the lake)

library(sp)
library(jpeg)
library(raster)

aral <- data.frame(x = 59.250327, 
                   y = 45.082526)
coordinates(aral) <- ~ x + y
proj4string(aral) <- "+init=epsg:4326"

web_img2000 <- "http://cdn.newsapi.com.au/image/v1/68565a36c0fccb1bc43c09d96e8fb029"

jpg2000 <- readJPEG(readBin(web_img2000, "raw", 1e6))

# Convert imagedata to raster
rst_blue2000 <- raster(jpg2000[, , 1])
rst_green2000 <- raster(jpg2000[, , 2])
rst_red2000 <- raster(jpg2000[, , 3])

img2000 <- brick(rst_red2000, rst_green2000, rst_blue2000)

web_img2013 <- "http://cdn.newsapi.com.au/image/v1/5707499d769db4b8ec76e8df61933f2a"

jpg2013 <- readJPEG(readBin(web_img2013, "raw", 1e6))

# Convert imagedata to raster
rst_blue2013 <- raster(jpg2013[, , 1])
rst_green2013 <- raster(jpg2013[, , 2])
rst_red2013 <- raster(jpg2013[, , 3])

img2013 <- brick(rst_red2013, rst_green2013, rst_blue2013)

slide <- slideView(img2000, img2013)

mapview(aral, map.types = "Esri.WorldImagery", popup = popupGraph(slide, type = "html"))

popupImage

Finally, it is possible to include local (on our local hard drive) or remote (from the web) images in popups

img <- system.file("img","Rlogo.png", package="png")
mapview(pnt, popup = popupImage(img))


img <- "https://upload.wikimedia.org/wikipedia/commons/thumb/6/62/Mount_Eden.jpg/640px-Mount_Eden.jpg"

mapview(pnt, map.types = "Esri.WorldImagery", popup = popupImage(img, src = "remote"))

All major image types are supported (jpg, png, and (animated) gif)

images <- c("https://upload.wikimedia.org/wikipedia/commons/thumb/6/62/Mount_Eden.jpg/640px-Mount_Eden.jpg",
            "https://www.r-project.org/logo/Rlogo.png",
            "https://upload.wikimedia.org/wikipedia/commons/d/d6/MeanMonthlyP.gif")

pt4 <- data.frame(x = jitter(rep(174.764474, 3), factor = 0.001),
                  y = jitter(rep(-36.877245, 3), factor = 0.001))
coordinates(pt4) <- ~ x + y
proj4string(pt4) <- "+init=epsg:4326"

mapview(pt4, popup = popupImage(images, src = "remote")) # NOTE the gif animation

There’s also a hidden function that enables us to include all sorts of things via Iframe (which is used internally when rendering htmlwidgets graphs).

But let the Dude show us how this can be used…

mapview(pnt, popup = mapview:::popupIframe("https://www.youtube.com/embed/iApz08Bh53w?autoplay=1", width = 300, height = 225))