From 3975ffe5a54d7726f2010657841f351553eb089a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Esteban=20Duhour?= Date: Tue, 17 Dec 2024 09:19:54 -0300 Subject: [PATCH 1/3] Add a table of visual variables and examples --- vignettes/01_basics_vv.Rmd | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/vignettes/01_basics_vv.Rmd b/vignettes/01_basics_vv.Rmd index 7be52d48e..a58583b6f 100644 --- a/vignettes/01_basics_vv.Rmd +++ b/vignettes/01_basics_vv.Rmd @@ -50,7 +50,7 @@ data(World, metro, rivers, land) #tmap_design_mode() ``` - +## Introduction ### About the data @@ -67,7 +67,7 @@ s = tm_shape(World, crs = "+proj=eqearth") ``` -## Constant values +### Constant values ```{r} s + tm_polygons(fill = "#ffce00", # fill color @@ -80,6 +80,12 @@ For advanced users: the default constant values are specified for combinations o ## Visual variables +A visual variable describes a certain visual property of a drawn object, such as color, size, shape, line width, line stroke, transparency, fill pattern (in **ggplot2** these are called aesthetics). +A visual variable can be specified using a constant value (e.g. \code{fill = "blue"}) or be **data-driven** (more on this later). If it can only be specified with a constant value, it is called a **visual constant**. + +The following table shows which visual variables are used in standard map layers. + + ```{r} s + tm_polygons(fill = "press") # data variable (column in World) @@ -95,11 +101,27 @@ s + ``` +## Scales + + ## Facets ```{r, fig.height = 8} s + tm_polygons(fill = c("well_being", "life_exp")) # two data variables (columns in World) ``` -(More on facets later) + + +## Legends + + +## Map modes + +## Base maps + +## Layout + +## Exporting maps + + From f9faab10f54e50e5020cd798aeeeca7ff49c5def Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Esteban=20Duhour?= Date: Tue, 17 Dec 2024 09:20:55 -0300 Subject: [PATCH 2/3] Add table of visual variables and examples --- vignettes/01_basics_vv.Rmd | 93 +++++++++++++++++++++++--------------- 1 file changed, 57 insertions(+), 36 deletions(-) diff --git a/vignettes/01_basics_vv.Rmd b/vignettes/01_basics_vv.Rmd index a58583b6f..1c50cad84 100644 --- a/vignettes/01_basics_vv.Rmd +++ b/vignettes/01_basics_vv.Rmd @@ -15,6 +15,8 @@ editor_options: ```{r, include = FALSE} knitr::opts_chunk$set( collapse = TRUE, + warning = FALSE, + message = FALSE, fig.width=6, fig.height=3, comment = "#>" @@ -43,7 +45,7 @@ knitr::knit_hooks$set(output = function(x, options) { ``` -```{r, echo = FALSE, message = FALSE} +```{r setup, echo = FALSE, message = FALSE} library(tmap) #devtools::load_all() data(World, metro, rivers, land) @@ -52,76 +54,95 @@ data(World, metro, rivers, land) ## Introduction -### About the data +**tmap** is an R package for spatial data visualization. -A spatial data object contained in tmap is called `World`. It is a data frame with a row for each country. The columns are the following data variables plus an additional geometry column which contains the geometries (see sf package): +### Map layers -```{r} -names(World) -``` +A (thematic) map consists of one or more map layers. Each map layer has a specific set of variables that determine how the objects of that layer are drawn. A **visual variable** +changes the appearance of a spatial object, e.g. fill color or line width. -We specify this object with `tm_shape` (see other vignette) and for convenience assign it to `s`: +### About the data -```{r} -s = tm_shape(World, crs = "+proj=eqearth") +A spatial data object contained in **tmap** is called `World`. It is a data frame with a row for each country. The columns are the following data variables plus an additional geometry column which contains the geometries (see sf package): + +```{r names} +names(World) ``` +We specify this object with `tm_shape` + +and for convenience assign it to `s`: -### Constant values +```{r shape} +s <- tm_shape(World, crs = "+proj=eqearth") -```{r} -s + tm_polygons(fill = "#ffce00", # fill color - col = "black", # line color - lwd = 0.5, # line width - lty = "dashed") # line type ``` -For advanced users: the default constant values are specified for combinations of visual variables and layer type. See `tmap_options("value.const")` - ## Visual variables -A visual variable describes a certain visual property of a drawn object, such as color, size, shape, line width, line stroke, transparency, fill pattern (in **ggplot2** these are called aesthetics). -A visual variable can be specified using a constant value (e.g. \code{fill = "blue"}) or be **data-driven** (more on this later). If it can only be specified with a constant value, it is called a **visual constant**. +A visual variable describes a certain visual property of a drawn object, such as color, size, shape, line width, line stroke, transparency, fill pattern (in **ggplot2** these are called _aesthetics_). +A visual variable can be specified using a constant value (e.g. `fill = "blue"`) or be **data-driven**. If it can only be specified with a constant value, it is called a **visual constant**. The following table shows which visual variables are used in standard map layers. +|Map layer |Visual variables |Visual constant | +|:--------------|:--------------------------------------------------------------|:------------------------------| +|`tm_basemap` |none |`alpha` (transparency level) | +|`tm_polygons` |`fill` (fill color), `col` (border color),
`lwd` (border line width), `lty` (border line type),
`fill_alpha` (fill transparency), `col_alpha`
(border color transparency) |`linejoin` (line join) and
`lineend` (line end) | +|`tm_symbols` |`fill` (fill color), `col` (border color), `size`, `shape`
`lwd` (border line width) `lty` (border line type),
`fill_alpha` (filltransparency), `col_alpha`
(border color transparency) | | +|`tm_lines` |`col` (color), `lwd` (line width) `lty` (line type),
`alpha` (transparency) |`linejoin` (line join) and
`lineend` (line end) | +|`tm_raster` |`col` (color), `col_alpha` (transparency) | | +|`tm_text` |`text` (the text itself), `size` (font size), `col` (color),
`fontface` (font face) | | + +Each visual variable argument can also be specified with a data variable (e.g., a column name). The following code add to `s` a layer of polygons and defines the visual variable `fill` with the data variable `press` (a column in `World`): -```{r} -s + tm_polygons(fill = "press") # data variable (column in World) +```{r polygons} +s + tm_polygons(fill = "press") ``` -```{r} +Add to `s` a layer of polygons with a constant fill color. Then add a layer of +symbols defining `size`, `fill` and `shape` with data variables in `World` + +```{r symbols} s + tm_polygons(fill = "grey90") + # fill color () - tm_bubbles(size = "pop_est", # data variable, mapped to symbol size + tm_symbols(size = "pop_est", # data variable, mapped to symbol size fill = "well_being", # data variable, mapped to symbol fill color shape = "income_grp") # data variable, mapped to symbol shape ``` -## Scales +The following code adds a polygon layer filled with the 'economy' data variable and a text layer mapping the 'name' data variable, with font size set according to the 'area' data variable. - -## Facets - -```{r, fig.height = 8} -s + tm_polygons(fill = c("well_being", "life_exp")) # two data variables (columns in World) +```{r text} +s + tm_polygons(fill = "economy") + + tm_text(text = "name", size = "area") ``` - +### Constant visual values -## Legends +The following code add a polygons layer assigning constant values to visual variables + +```{r constant} +s + tm_polygons(fill = "#ffce00", # fill color + col = "black", # line color + lwd = 0.5, # line width + lty = "dashed") # line type +``` +For advanced users: the default constant values are specified for combinations of visual variables and layer type. See `tmap_options("value.const")` -## Map modes -## Base maps +## Facets -## Layout +A facet map is created by specifying two data variables (columns in `World`) to the visual variable `fill`: -## Exporting maps - +```{r facet} +s + tm_polygons(fill = c("well_being", "life_exp")) +``` + + From ef79d1bae899e66fd42c388d4ddf038df72a11fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Esteban=20Duhour?= Date: Tue, 17 Dec 2024 23:10:51 -0300 Subject: [PATCH 3/3] update --- vignettes/01_basics_vv.Rmd | 58 +++++++++++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 4 deletions(-) diff --git a/vignettes/01_basics_vv.Rmd b/vignettes/01_basics_vv.Rmd index 1c50cad84..b394d642f 100644 --- a/vignettes/01_basics_vv.Rmd +++ b/vignettes/01_basics_vv.Rmd @@ -15,10 +15,16 @@ editor_options: ```{r, include = FALSE} knitr::opts_chunk$set( collapse = TRUE, +<<<<<<< HEAD warning = FALSE, message = FALSE, fig.width=6, fig.height=3, +======= + out.width = "100%", + dpi = 300, + fig.width = 7.2916667, +>>>>>>> upstream/master comment = "#>" ) hook_output <- knitr::knit_hooks$get("output") @@ -47,9 +53,7 @@ knitr::knit_hooks$set(output = function(x, options) { ```{r setup, echo = FALSE, message = FALSE} library(tmap) -#devtools::load_all() -data(World, metro, rivers, land) -#tmap_design_mode() +tmap_options(scale = 0.75) ``` ## Introduction @@ -61,7 +65,7 @@ data(World, metro, rivers, land) A (thematic) map consists of one or more map layers. Each map layer has a specific set of variables that determine how the objects of that layer are drawn. A **visual variable** changes the appearance of a spatial object, e.g. fill color or line width. -### About the data +## About the data A spatial data object contained in **tmap** is called `World`. It is a data frame with a row for each country. The columns are the following data variables plus an additional geometry column which contains the geometries (see sf package): @@ -105,6 +109,7 @@ s + tm_polygons(fill = "press") Add to `s` a layer of polygons with a constant fill color. Then add a layer of symbols defining `size`, `fill` and `shape` with data variables in `World` +<<<<<<< HEAD ```{r symbols} s + tm_polygons(fill = "grey90") + # fill color () @@ -130,10 +135,20 @@ s + tm_polygons(fill = "#ffce00", # fill color col = "black", # line color lwd = 0.5, # line width lty = "dashed") # line type +======= +```{r, fig.height = 3.5} +s + + tm_polygons( + fill = "#ffce00", # fill color + col = "black", # line color + lwd = 0.5, # line width + lty = "dashed") # line type +>>>>>>> upstream/master ``` For advanced users: the default constant values are specified for combinations of visual variables and layer type. See `tmap_options("value.const")` +<<<<<<< HEAD ## Facets @@ -141,6 +156,41 @@ A facet map is created by specifying two data variables (columns in `World`) to ```{r facet} s + tm_polygons(fill = c("well_being", "life_exp")) +======= +## Visual variables + + +```{r, fig.height = 3.5} +s + tm_polygons(fill = "press") # data variable (column in World) +``` + + +```{r, fig.height = 5} +s + + tm_polygons(fill = "grey90") + # fill color () + tm_bubbles( + size = "pop_est", # data variable, mapped to symbol size + fill = "well_being", # data variable, mapped to symbol fill color + shape = "income_grp") # data variable, mapped to symbol shape + +``` + +## Facets + +```{r, fig.height = 8} +s + + tm_polygons(fill = c("well_being", "life_exp")) +``` + +Improve the titles and remove (duplicate) legend titles + +```{r, fig.height = 8} +s + + tm_polygons( + fill = c("well_being", "life_exp"), + fill.legend = tm_legend("")) + +tm_layout(panel.labels = c("Well Being", "Life Expectancy")) +>>>>>>> upstream/master ```