-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added mapview vig, improved view-gradient legend
- Loading branch information
Showing
9 changed files
with
214 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
--- | ||
title: "tmap versus: mapview" | ||
output: | ||
bookdown::html_vignette2: | ||
pkgdown: | ||
as_is: true | ||
template: | ||
math-rendering: mathjax | ||
bibliography: '`r system.file("tmap.bib", package="tmap")`' | ||
csl: "`r system.file('ieee.csl', package = 'tmap')`" | ||
editor_options: | ||
chunk_output_type: console | ||
--- | ||
|
||
```{r, include = FALSE} | ||
knitr::opts_chunk$set( | ||
collapse = TRUE, | ||
out.width = "100%", | ||
dpi = 300, | ||
fig.width = 7.2916667, | ||
comment = "#>" | ||
) | ||
hook_output <- knitr::knit_hooks$get("output") | ||
knitr::knit_hooks$set(output = function(x, options) { | ||
lines <- options$output.lines | ||
if (is.null(lines)) { | ||
return(hook_output(x, options)) # pass to default hook | ||
} | ||
x <- unlist(strsplit(x, "\n")) | ||
more <- "..." | ||
if (length(lines)==1) { # first n lines | ||
if (length(x) > lines) { | ||
# truncate the output, but add .... | ||
x <- c(head(x, lines), more) | ||
} | ||
} else { | ||
x <- c(more, x[lines], more) | ||
} | ||
# paste these lines together | ||
x <- paste(c(x, ""), collapse = "\n") | ||
hook_output(x, options) | ||
}) | ||
``` | ||
|
||
|
||
```{r, echo = FALSE, message = FALSE} | ||
library(tmap) | ||
tmap_options(scale = 1) | ||
``` | ||
|
||
## Mapview | ||
|
||
Mapview is an excellent R package for interactive maps. When to use which package? This is what ChatGPT answered: | ||
|
||
| Use Case | Recommended Package | | ||
|-------------------------------------------|----------------------| | ||
| High-quality thematic maps for publication | `tmap` | | ||
| Quick exploratory visualization of spatial data | `mapview` | | ||
| Need both static and interactive maps | `tmap` | | ||
| Debugging or inspecting spatial datasets | `mapview` | | ||
| Comparing multiple variables or time steps | `tmap` (facets) | | ||
| Large datasets for interactive exploration | `mapview` | | ||
|
||
Pretty accurate I would say. In addition: | ||
* tmap has a more sophisticated/complex syntax that is based on the [grammar of graphics](21_theory_gg) and is therefore popular for education purposes | ||
* mapview is a front runner in terms of interactive features and is probably a bit faster. | ||
|
||
Worth noting is that the development teams of tmap and mapview collaborate a lot, which also holds for R-spatial developers in general. | ||
|
||
## Modes / platforms | ||
|
||
* tmap offers two modes: "plot" and "view" (but is extendable, see https://github.com/r-tmap/tmap.deckgl). | ||
* mapview supports three modes, which they call *platforms*: "leaflet", "leafgl", and "mapdeck". | ||
|
||
tmap "view" (with `tm_view(use_WebGL = FALSE)`) is similar to mapview "leaflet" | ||
tmap "view" (with `tm_view(use_WebGL = TRUE)`) is similar to mapview "leafgl" | ||
|
||
tmap does not offer a mode using Mapbox yet | ||
|
||
|
||
## Default maps | ||
|
||
This is the default output of `mapview`: | ||
|
||
```{r fig.height = 3.5} | ||
library(mapview) | ||
mapview(World) | ||
``` | ||
|
||
|
||
This is the default output of `tmap`: | ||
|
||
```{r fig.height = 3.5} | ||
tmap_mode("view") | ||
qtm(World) # qmt stands for 'quick thematic map' | ||
``` | ||
|
||
## Choropleth | ||
|
||
|
||
```{r fig.height = 3.5} | ||
mapview(World, z = "HPI") | ||
``` | ||
|
||
```{r fig.height = 3.5} | ||
tm_shape(World) + | ||
tm_polygons(fill = "HPI") | ||
``` | ||
|
||
|
||
## Mimicking mapview layout | ||
|
||
We can use `tmap` to match the style of `mapview`: | ||
|
||
```{r fig.height = 3.5} | ||
tm_shape(World) + | ||
tm_polygons( | ||
fill = "HPI", | ||
fill_alpha = 0.6, | ||
col_alpha = 0.9, | ||
fill.legend = tm_legend( | ||
title = "World - HPI", | ||
position = c("right", "top"), | ||
fill_alpha = 1), | ||
fill.scale = tm_scale_continuous(values = "viridis", n = 7, value.na = "#BEBEBE") | ||
) + | ||
tm_basemap(c("CartoDB.Positron", "CartoDB.DarkMatter", | ||
"OpenStreetMap", "Esri.WorldImagery", "OpenTopoMap")) | ||
``` | ||
|
||
|
Oops, something went wrong.