4 mapview

Nowadays maps are often integrated into websites. There are also packages in R for this type of application. The mapview package was created with the goal of quickly creating interactive maps. The most important function is mapview() which is used to generate the maps.

# for loading our data
library(sf)
# for plotting
library(lattice)
library(leafpop)
library(mapview)
library(vapoRwave)
library(viridis)

4.1 Data used

In this chapter we will only be using the bavaria dataset. So let’s load that real quick.

bavaria <- read_sf("bavaria.shp")

4.2 Using mapview to create maps

The easiest way to create a map in mapview is to use the mapview() functions and pass a dataset to it.

mapview(bavaria)

This map already contains some basic functionality. We can select different basemaps, and when we click on one of the polygons, a table displays additional information about the city or district.

We can use the zcol argument to color our polygons depending on the value of a variable.

mapview(bavaria, zcol = "unemployment_rate")

If we want to use a custom palette, we can define one and then use the col.regions argument.

pal <- magma(n = length(unique(bavaria$employment_rate)), direction = -1)
mapview(bavaria, zcol = "unemployment_rate", col.regions = pal)

The at argument can be used to define your own breakpoints.

mapview(bavaria, zcol = "unemployment_rate", col.regions = jazzCup_pal(), at = c(0, 2, 4, 6, 8))

To visualize several maps at the same time, several mapview() functions can be connected with +. This way the European elevation raster map from chapter 2 can be reproduced. We use alpha.regions so that only the outlines of the countries are visible, and make the size of the circles dependent on the pop variable by using the cex argument.

mapview(europe_raster, legend = FALSE) +
  mapview(europe_shape, legend = FALSE, alpha.regions = 0) +
  mapview(cities, legend = FALSE, cex = "pop")

With this card several things stand out immediately. The complete shapefile is shown, not only the areas within the grid. For example, Svalbard and the French overseas territories are shown. In the lower right corner you can click on cities, europe_shape or europe_raster to focus on one of these areas. By clicking on one of the cities or countries, a popup opens with further information about the city or country.

If the argument burst = TRUE is used in addition to zcol, a single layer is created for each unique value of the selected variable, which can be focused or hidden. To focus on a layer, you can click on any of the names in the lower right corner. To hide a layer, you can use the layer controls in the upper left corner.

mapview(highways, color = "red", lwd = 3,
        layer.name = "highways", legend = FALSE) +
  mapview(bavaria, zcol = "admin_district", burst = TRUE, col.regions = brewer.pal(7, "Dark2")) +
  mapview(airports, legend = FALSE, col.regions = "black")

The functions popupTable() and popupGraph() from the leafpop package can be used to specify which variables should appear in the popup or to display a graph. In the following example we are only interested in the unemployment rate, household income and population density. So we only include these variables in the tables that are displayed when you click on a polygon.

mapview(
  bavaria,
  zcol = "admin_district",
  col.regions = brewer.pal(7, "Dark2"),
  popup = popupTable(
    bavaria,
    zcol = c(
      "unemployment_rate",
      "household_income",
      "pop_density"
      )
    )
  )

To display a custom plot when you click on a polygon, we must first create a plot for each city and district. For each point in our dataset, we create a scatterplot in which the current data point is displayed in red, while the remaining points are grey.

p <- xyplot(pop_density ~ unemployment_rate, data = bavaria, col = "grey", pch = 20, cex = 2)
p <- mget(rep("p", nrow(bavaria)))

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

We then use the popup argument to use our custom plots as popups.

mapview(bavaria, zcol = "unemployment_rate", col.regions = pal, popup = popupGraph(p))

With the functions leafpop::popupImage() or leafpop:::popupIframe() even images, hyperlinks and YouTube videos can be integrated.