Skip to content

Commit

Permalink
Merge branch 'main' into ch10-pr
Browse files Browse the repository at this point in the history
  • Loading branch information
Nowosad authored Sep 25, 2024
2 parents c6d02b1 + 560b8c3 commit f1505f7
Show file tree
Hide file tree
Showing 15 changed files with 77 additions and 73 deletions.
62 changes: 32 additions & 30 deletions 01-introduction.Rmd

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions 02-spatial-data.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ Simple features is an [open standard](http://portal.opengeospatial.org/files/?ar
Simple features is a hierarchical data model that represents a wide range of geometry types.
Of 18 geometry types supported by the specification, only 7 are used in the vast majority of geographic research (see Figure \@ref(fig:sf-ogc));
these core geometry types are fully supported by the R package **sf** [@pebesma_simple_2018].^[
The full OGC standard includes rather exotic geometry types including 'surface' and 'curve' geometry types, which currently have limited application in real world applications.
The full OGC standard includes rather exotic geometry types including 'surface' and 'curve' geometry types, which currently have limited application in real-world applications.
You can find the whole list of possible feature types in [the PostGIS manual ](http://postgis.net/docs/using_postgis_dbmanagement.html).
All 18 types can be represented with the **sf** package, although at the time of writing (2024) plotting only works for the 'core 7'.
]
Expand Down Expand Up @@ -290,13 +290,13 @@ As described in Chapter \@ref(attr), which shows how to manipulate `sf` objects
**spatstat**, a package ecosystem which provides numerous functions for spatial statistics, and **terra** both have vector geographic data classes, but neither have the same level of uptake as **sf** does for working with vector data.
Many popular packages build on **sf**, as shown by the rise in its popularity in terms of number of downloads per day, as shown in Section \@ref(r-ecosystem) in the previous chapter.

### Basic map making {#basic-map}
### Basic map-making {#basic-map}

Basic maps are created in **sf** with `plot()`.
By default this creates a multi-panel plot, one sub-plot for each variable of the object, as illustrated in the left-hand panel in Figure \@ref(fig:sfplot).
A legend or 'key' with a continuous color is produced if the object to be plotted has a single variable (see the right-hand panel).
Colors can also be set with `col = `, although this will not create a continuous palette or a legend.
\index{map making!basic}
\index{map-making!basic}

```{r sfplot, fig.cap="Plotting with sf, with multiple variables (left) and a single variable (right).", out.width="49%", fig.show='hold', warning=FALSE, fig.scap="Plotting with sf."}
plot(world[3:6])
Expand Down Expand Up @@ -326,13 +326,13 @@ plot(asia, add = TRUE, col = "red")
```{block2 plottingpacks, type='rmdnote'}
Adding layers in this way can be used to verify the geographic correspondence between layers:
the `plot()` function is fast to execute and requires few lines of code, but does not create interactive maps with a wide range of options.
For more advanced map making we recommend using dedicated visualization packages such as **tmap** [@tmap2018] (see Chapter \@ref(adv-map)).
For more advanced map-making we recommend using dedicated visualization packages such as **tmap** [@tmap2018] (see Chapter \@ref(adv-map)).
```

There are various ways to modify maps with **sf**'s `plot()` method.
Because **sf** extends base R plotting methods, `plot()`'s arguments work with `sf` objects (see `?graphics::plot` and `?par` for information on arguments such as `main =`).^[
Note: many plot arguments are ignored in facet maps, when more than one `sf` column is plotted.]
\index{base plot|see {map making}}\index{map making!base plotting} Figure \@ref(fig:contpop) illustrates this flexibility by overlaying circles, whose diameters (set with `cex =`) represent country populations, on a map of the world.
\index{base plot|see {map-making}}\index{map-making!base plotting} Figure \@ref(fig:contpop) illustrates this flexibility by overlaying circles, whose diameters (set with `cex =`) represent country populations, on a map of the world.
An unprojected version of this figure can be created with the following commands (see exercises at the end of this chapter and the script [`02-contplot.R`](https://github.com/geocompx/geocompr/blob/main/code/02-contpop.R) to reproduce Figure \@ref(fig:contpop)):

```{r 02-spatial-data-16, eval=FALSE}
Expand Down Expand Up @@ -707,7 +707,7 @@ Every core **sfheaders** function has a corresponding C++ implementation, as des
For most people, the R functions will be more than sufficient to benefit from the computational speed of the package.
**sfheaders** was developed separately from **sf**, but aims to be fully compatible, creating valid `sf` objects of the type described in preceding sections.

The simplest use-case for **sfheaders** is demonstrated in the code chunks below with examples of building `sfg`, `sfc`, and `sf` objects showing:
The simplest use case for **sfheaders** is demonstrated in the code chunks below with examples of building `sfg`, `sfc`, and `sf` objects showing:

- A vector converted to `sfg_POINT`
- A matrix converted to `sfg_LINESTRING`
Expand Down Expand Up @@ -899,7 +899,7 @@ source("code/02-raster-intro-plot2.R", print.eval = TRUE)

Over the last two decades, several packages for reading and processing raster datasets have been developed.
\index{raster (package)}\index{terra (package)}\index{stars (package)}
As outlined in Section \@ref(the-history-of-r-spatial), chief among them was **raster**, which led to a step change in R's raster capabilities when it was launched in 2010 and the premier package in the space until the development of **terra** and **stars**.
As outlined in Section \@ref(history-of-r-spatial), chief among them was **raster**, which led to a step change in R's raster capabilities when it was launched in 2010 and the premier package in the space until the development of **terra** and **stars**.
Both more recently developed packages provide powerful and performant functions for working with raster datasets and there is substantial overlap between their possible use cases.
In this book we focus on **terra**, which replaces the older and (in most cases) slower **raster**.
Before learning about the how **terra**'s class system works, this section describes similarities and differences between **terra** and **stars**; this knowledge will help decide which is most appropriate in different situations.
Expand Down Expand Up @@ -957,10 +957,10 @@ Dedicated functions report each component: `dim()` returns the number of rows, c
`help("terra-package")` returns a full list of all available **terra** functions.
```

### Basic map making {#basic-map-raster}
### Basic map-making {#basic-map-raster}

Similar to the **sf** package, **terra** also provides `plot()` methods for its own classes.
\index{map making!basic raster}
\index{map-making!basic raster}

```{r basic-new-raster-plot, fig.cap="Basic raster plot."}
plot(my_rast)
Expand Down
2 changes: 1 addition & 1 deletion 08-read-write-plot.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ Additional examples are available through vignettes, such as [how to access rast

## Visual outputs

\index{map making!outputs}
\index{map-making!outputs}
R supports many different static and interactive graphics formats.
Chapter \@ref(adv-map) covers map-making in detail, but it is worth mentioning ways to output visualizations here.
The most general method to save a static plot is to open a graphic device, create a plot, and close it, for example:
Expand Down
38 changes: 19 additions & 19 deletions 09-mapping.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,26 @@ nz_elev = rast(system.file("raster/nz_elev.tif", package = "spDataLarge"))
## Introduction

A satisfying and important aspect of geographic research is communicating the results.
Map making\index{map making} --- the art of cartography --- is an ancient skill involving communication, attention to detail, and an element of creativity.
Map-making\index{map-making} --- the art of cartography --- is an ancient skill involving communication, attention to detail, and an element of creativity.
Static mapping in R is straightforward with the `plot()` function, as we saw in Section \@ref(basic-map).
It is possible to create advanced maps using base R methods [@murrell_r_2016].
The focus of this chapter, however, is cartography with dedicated map-making packages.
When learning a new skill, it makes sense to gain depth-of-knowledge in one area before branching out.
Map making is no exception, hence this chapter's coverage of one package (**tmap**) in depth rather than many superficially.
Map-making is no exception, hence this chapter's coverage of one package (**tmap**) in depth rather than many superficially.

In addition to being fun and creative, cartography also has important practical applications.
A carefully crafted map can be the best way of communicating the results of your work, but poorly designed maps can leave a bad impression.
Common design issues include poor placement, size and readability of text and careless selection of colors, as outlined in the style [guide](https://files.taylorandfrancis.com/TJOM-suppmaterial-quick-guide.pdf) of the Journal of Maps.
Furthermore, poor map making can hinder the communication of results [@brewer_designing_2015]:
Furthermore, poor map-making can hinder the communication of results [@brewer_designing_2015]:

> Amateur-looking maps can undermine your audience’s ability to understand important information and weaken the presentation of a professional data investigation.
Maps have been used for several thousand years for a wide variety of purposes.
Historic examples include maps of buildings and land ownership in the Old Babylonian dynasty more than 3000 years ago and Ptolemy's world map in his masterpiece *Geography*\index{geography} nearly 2000 years ago [@talbert_ancient_2014].

Map making has historically been an activity undertaken only by, or on behalf of, the elite.
Map-making has historically been an activity undertaken only by, or on behalf of, the elite.
This has changed with the emergence of open source mapping software such as the R package **tmap** and the 'print layout' in QGIS\index{QGIS} which enable anyone to make high-quality maps, enabling 'citizen science'.
Maps are also often the best way to present the findings of geocomputational research in a way that is accessible.
Map making is therefore a critical part of geocomputation\index{geocomputation} and its emphasis not only on describing, but also *changing* the world.
Map-making is therefore a critical part of geocomputation\index{geocomputation} and its emphasis not only on describing, but also *changing* the world.

This chapter shows how to make a wide range of maps.
The next section covers a range of static maps, including aesthetic considerations, facets and inset maps.
Expand All @@ -72,7 +72,7 @@ Finally, Section \@ref(other-mapping-packages) covers a range of alternative map

## Static maps

\index{map making!static maps}
\index{map-making!static maps}
Static maps are the most common type of visual output from geocomputation.
They are usually stored in standard formats including `.png` and `.pdf` for *graphical* raster and vector outputs, respectively.
Initially, static maps were the only type of maps that R could produce.
Expand Down Expand Up @@ -119,8 +119,8 @@ source("https://github.com/geocompx/geocompr/raw/main/code/09-tmshape.R", print.
The object passed to `tm_shape()` in this case is `nz`, an `sf` object representing the regions of New Zealand (see Section \@ref(intro-sf) for more on `sf` objects).
Layers are added to represent `nz` visually, with `tm_fill()` and `tm_borders()` creating shaded areas (left panel) and border outlines (middle panel) in Figure \@ref(fig:tmshape), respectively.

\index{map making!layers}
This is an intuitive approach to map making:
\index{map-making!layers}
This is an intuitive approach to map-making:
the common task of *adding* new layers is undertaken by the addition operator `+`, followed by `tm_*()`.
The asterisk (\*) refers to a wide range of layer types which have self-explanatory names including:

Expand Down Expand Up @@ -184,7 +184,7 @@ map_nz3 = map_nz2 +
tm_shape(nz_height) + tm_symbols()
```

\index{map making!metaplot}
\index{map-making!metaplot}
A useful and little known feature of **tmap** is that multiple map objects can be arranged in a single 'metaplot' with `tmap_arrange()`.
This is demonstrated in the code chunk below which plots `map_nz1` to `map_nz3`, resulting in Figure \@ref(fig:tmlayers).

Expand All @@ -197,8 +197,8 @@ Aesthetic settings, however, are controlled by arguments to layer functions.

### Visual variables

\index{map making!aesthetics}
\index{map making!visual variables}
\index{map-making!aesthetics}
\index{map-making!visual variables}
The plots in the previous section demonstrate **tmap**'s default aesthetic settings.
Gray shades are used for `tm_fill()` and `tm_symbols()` layers and a continuous black line is used to represent lines created with `tm_lines()`.
Of course, these default values and other aesthetics can be overridden.
Expand Down Expand Up @@ -364,7 +364,7 @@ For example run `tmap_options()$values.var`.
```

\index{color palettes}
There are three main groups of color palettes\index{map making!color palettes}: categorical, sequential and diverging (Figure \@ref(fig:colpal)), and each of them serves a different purpose.^[
There are three main groups of color palettes\index{map-making!color palettes}: categorical, sequential and diverging (Figure \@ref(fig:colpal)), and each of them serves a different purpose.^[
A fourth group of color palettes, called bivariate, also exists.
They are used when we want to represent relations between two variables on one map.
]
Expand Down Expand Up @@ -520,7 +520,7 @@ source("code/09-layout2.R", print.eval = TRUE)

### Faceted maps

\index{map making!faceted maps}
\index{map-making!faceted maps}
\index{tmap (package)!faceted maps}
Faceted maps, also referred to as 'small multiples', are composed of many maps arranged side-by-side, and sometimes stacked vertically [@meulemans_small_2017].
Facets enable the visualization of how spatial relationships change with respect to another variable, such as time.
Expand Down Expand Up @@ -555,7 +555,7 @@ In addition to their utility for showing changing spatial relationships, faceted

### Inset maps

\index{map making!inset maps}
\index{map-making!inset maps}
\index{tmap (package)!inset maps}
An inset map is a smaller map rendered within or next to the main map.
It could serve many different purposes, including providing a context (Figure \@ref(fig:insetmap1)) or bringing some non-contiguous regions closer to ease their comparison (Figure \@ref(fig:insetmap2)).
Expand Down Expand Up @@ -678,7 +678,7 @@ For a more in-depth approach, see the [`us-map`](https://geocompx.github.io/geoc

## Animated maps

\index{map making!animated maps}
\index{map-making!animated maps}
\index{tmap (package)!animated maps}
Faceted maps, described in Section \@ref(faceted-maps), can show how spatial distributions of variables change (e.g., over time), but the approach has disadvantages.
Facets become tiny when there are many of them.
Expand Down Expand Up @@ -747,11 +747,11 @@ if (knitr::is_latex_output()){

## Interactive maps

\index{map making!interactive maps}
\index{map-making!interactive maps}
\index{tmap (package)!interactive maps}
While static and animated maps can enliven geographic datasets, interactive maps can take them to a new level.
Interactivity can take many forms, the most common and useful of which is the ability to pan around and zoom into any part of a geographic dataset overlaid on a 'web map' to show context.
Less advanced interactivity levels include popups which appear when you click on different features, a kind of interactive label.
Less advanced interactivity levels include pop-ups which appear when you click on different features, a kind of interactive label.
More advanced levels of interactivity include the ability to tilt and rotate maps, as demonstrated in the **mapdeck** example below, and the provision of "dynamically linked" sub-plots which automatically update when the user pans and zooms [@pezanowski_senseplace3_2018].

The most important type of interactivity, however, is the display of geographic data on interactive or 'slippy' web maps.
Expand Down Expand Up @@ -942,7 +942,7 @@ if (knitr::is_latex_output() || knitr::is_html_output()){

## Mapping applications

\index{map making!mapping applications}
\index{map-making!mapping applications}
The interactive web maps demonstrated in Section \@ref(interactive-maps) can go far.
Careful selection of layers to display, base-maps and pop-ups can be used to communicate the main results of many projects involving geocomputation.
But the web mapping approach to interactivity has limitations:
Expand Down Expand Up @@ -1051,7 +1051,7 @@ knitr::include_graphics("images/09_cycle_hire_app.png")

**tmap** provides a powerful interface for creating a wide range of static maps (Section \@ref(static-maps)) and also supports interactive maps (Section \@ref(interactive-maps)).
But there are many other options for creating maps in R.
The aim of this section is to provide a taster of some of these and pointers for additional resources: map making is a surprisingly active area of R package development, so there is more to learn than can be covered here.
The aim of this section is to provide a taster of some of these and pointers for additional resources: map-making is a surprisingly active area of R package development, so there is more to learn than can be covered here.

The most mature option is to use `plot()` methods provided by core spatial packages **sf** and **terra**, covered in Sections \@ref(basic-map) and \@ref(basic-map-raster), respectively.
What we have not mentioned in those sections was that plot methods for vector and raster objects can be combined when the results draw onto the same plot area (elements such as keys in **sf** plots and multi-band rasters will interfere with this).
Expand Down
2 changes: 1 addition & 1 deletion 10-gis.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,7 @@ The most important open source spatial database\index{spatial database} is PostG
SQLite/SpatiaLite are certainly also important, but implicitly we have already introduced this approach since GRASS GIS\index{GRASS GIS} is using SQLite in the background (see Section \@ref(grass)).
]
R bridges to spatial DBMSs such as PostGIS\index{PostGIS} are important, allowing access to huge data stores without loading several gigabytes of geographic data into RAM, and likely crashing the R session.
The remainder of this section shows how PostGIS can be called from R, based on "Hello real world" from *PostGIS in Action, Second Edition* [@obe_postgis_2015].^[
The remainder of this section shows how PostGIS can be called from R, based on "Hello real-world" from *PostGIS in Action, Second Edition* [@obe_postgis_2015].^[
Thanks to Manning Publications, Regina Obe and Leo Hsu for permission to use this example.
]

Expand Down
Loading

0 comments on commit f1505f7

Please sign in to comment.