New release of mapsf

mapsf
Author

Timothée Giraud

Published

July 11, 2023

A minor update of mapsf has been deployed on CRAN. In this article I’ll describe the changes introduced by this update.

New features

expandBB

An expandBB argument has been introduced in mf_map(). This argument allows to add some space around a map.

The following code displays a map with some extra space on the right side of the figure. This can be useful to add legends or annotations.

library(mapsf)
library(sf)
nc_raw <- st_read(system.file("shape/nc.shp", package="sf"), quiet = TRUE)
nc <- st_transform(nc_raw, "EPSG:32019")
th <- mf_theme("candy", mar = c(0, 0, 0, 0), bg = "ivory")
mf_map(nc, expandBB = c(0, 0, 0, .3), width = 3.5)

# mf_get_ratio(nc[1,], expandBB = c(0,0.5,0,0), width = 3.5)

This addition makes the mf_init() function virtually useless. These two script produce the same output.

par(mfrow = c(1,2))
# with mf_init()
mf_init(nc[1, ],  expandBB = c(0, .5, 0, 0))
mf_map(nc, add = TRUE)
mf_map(nc[1, ], col = "red", add = TRUE)
# without mf_init()
mf_map(nc[1, ], col = NA, border = NA, expandBB = c(0, .5, 0, 0))
mf_map(nc, add = TRUE)
mf_map(nc[1, ], col = "red", add = TRUE)

This argument has also been added to mf_raster().

mf_graticule()

A new mf_graticule() function has been added to the package.

mf_map(nc, expandBB = c(0, .05, .1, 0))
mf_graticule(nc, add = TRUE)

Graticules labels can be added on the four sides of the map.

mf_map(nc, expandBB = c(.1, .05, .1, .05))
mf_graticule(nc, 
             pos = c("bottom", "left", "top", "right"), 
             lty = 2, 
             lwd = 2, 
             add = TRUE)

Warning

From st_graticule():
In cartographic visualization, the use of graticules is not advised, unless the graphical output will be used for measurement or navigation, or the direction of North is important for the interpretation of the content, or the content is intended to display distortions and artifacts created by projection. Unnecessary use of graticules only adds visual clutter but little relevant information. Use of coastlines, administrative boundaries or place names permits most viewers of the output to orient themselves better than a graticule.

Fixes

Pipe

The pipe compatibility of mapsf has been enhanced and it now works on most use cases.

mf_get_mtq() |>
  mf_map(col = "ivory4") |>
  mf_map("POP", "prop") |>
  mf_graticule(label = FALSE, lwd = .2)

Themes

The graphic theme feature has been refactored. The current theme parameters are now accessible via R options (with the mapsf. prefix).

(mf_theme("green"))
$bg
[1] "#1B1D16"

$fg
[1] "#D7FF68"

$mar
[1] 0.5 0.5 2.0 0.5

$tab
[1] FALSE

$pos
[1] "center"

$inner
[1] FALSE

$line
[1] 1.5

$cex
[1] 1

$font
[1] 2
getOption("mapsf.bg")
[1] "#1B1D16"
getOption("mapsf.fg")
[1] "#D7FF68"
getOption("mapsf.pos")
[1] "center"
# etc

The current theme is applied to all maps and there is no need to use the theme argument in mf_export() or mf_init() anymore.

Next steps

The next release of mapsf will use the maplegend package (not on CRAN yet) for plotting legends. This will allow to plot horizontal legends (#19) and continuous legends for rasters (#22).


See the NEWS file for the complete list of changes.