The mapsf package

 mapsf has just landed on CRAN.

mapsf helps to design various cartographic representations such as proportional symbols, choropleth or typology maps. It also offers several functions to display layout elements that improve the graphic presentation of maps (e.g. scale bar, north arrow, title, labels). mapsf maps sf objects on base graphics.

This package has a website  and a vignette.

Ce post est disponible en français ici.

Main features

mf_map()

mf_map() is the main function of mapsf, it displays map layers on a georeferenced plot. It has three main arguments: x, an sf object, var, the name(s) of a variable(s) to map and type, the map layer type.

Example of choropleth map:

Here are examples of each type of map:

Map Layout

Along with the cartographic functions, some other functions are dedicated to layout design (e.g. legends, scale bar, north arrow, title, credits…).

Themes

mf_theme() defines a map theme. It sets the margins of the figure, a background color, a foreground color and some title parameters. A set of theme is included in the package and it is possible to create custom ones.

Here are the built-in themes:

Insets

One can create an inset map with mf_inset_on().

With mf_inset_on(x = "worldmap") and mf_worldmap() a world scale situation map is created:

Inset are not limited to maps, in this example we have created an inset to display a “histogram-legend”.

Map export

mf_init() plots an invisible layer with the extent of a spatial object.

Note the use of expandBB to expand the map space on one (or several) side of the figure.  This argument can be useful to save some space to display legends or insets.

The mf_init() function can be used to export maps in raster (PNG) or vector formats (SVG). The size of the exported map will fit the height/width ratio of a spatial object (x). It allows to produce well fitted figures without excessive margins.

Syntactic Sugar

mapsf is,  mostly, compatible with the pipe syntax (%>% from magrittr, and likely the future base |>) :

Développement

The development of mapsf happens on GitHub. This project uses conventional commits that force the creation of an explicit commit history. 
Unlike cartography, mapsf will not be enriched by “exotic” map representations (waffle maps, dicontinuities, map tiles…).  If these mapping methods are needed, they will be developped in one or several complementary packages (e.g. maptiles for downloading and displaying map tiles).

But cartography

cartography was created in 2015. It was based on sp, rgdal and rgeos. V2.0.0 introduced sf compatibility and internally switched to sf functions and objects. But the general API was not significatly modified.

Some initial choices (camelCase, ultra-verbose function and argument names) were not the most inspired. This, plus the need to ensure a correct minimum backward compatibility (132k downloads for cartography through the RStudio CRAN), tend to hinder the development of new feature.

So, mapsf is the successor of cartography. There are no plans for new features or enhancements in cartography, but basic maintenance and support will continue indefinitely. Existing projects that use cartography can safely continue to use cartography. New projects should use mapsf because it is friendlier, lighter and more robust.
See mapsf vignette to migrate from cartography to mapsf.

Acknowledgements

I would like to thank Hugues Pecout for its beautiful hex logo and its always relevant feedback on the user-friendliness of the package website, Diego Hernangómez for its work on the design of package website, and of course authors and contributors of the sf package and the essential GEOS, GDAL and PROJ libraries.



Citer ce billet
Timothée Giraud (2021, 4 mars). The mapsf package. R Géomatique. Consulté le 19 avril 2024, à l’adresse https://doi.org/10.58079/tp4p

9 réflexions sur « The mapsf package »

  1. Bonjour,
    Tout d’abord merci pour votre package qui m’est très utile dans les productions que je réalise. J’ai deux questions à vous poser :
    – La barre d’échelle affiche une valeur incohérente (par exemple, 3e-04 pour un département comme la Seine-Maritime). Je vois dans l’aide de la fonction mf_scale que le problème peut venir de la projection. Avez vous une idée de comment la corriger ? J’ai préparé ma couche avec Qgis.
    – Sur les cercles proportionnelles, est-il possible de régler la transparence de la couleur à l’intérieur des cercles ?

    En vous remerciant pour votre aide.

    1. Bonjour,
      – Comme l’indique la documentation (“This scale bar is not accurate on unprojected (long/lat) maps.”) la fonction mf_scale() ne fonctionne pas avec des données non projetées (en longitude / latitude, comme le WGS84 par exemple). La solution est donc d’utiliser un système de référence de coordonnées (CRS) projetées (“EPSG:2154”, c’est à dire Lambert 93, pour la France).
      – Vous pouvez indiquer des couleurs au format hexadécimal pour la couleur des cercles : par exemple “#FF000080” pour un rouge avec 50% de transparence.
      T.

  2. Is there mapsf functionality or workarounds for choropleth fill patterns ( hatching, stippling, etc.) ? It is very convenient for 2-dimensional data in grayscale.

  3. Hi! Thanks for your website, it helps me a lot. I am beggining in cartography (i am a mathematician). mtq is for Martinique, if i would like the same for Gironde (France) how can i do? Thank you

    1. The mtq dataset is shipped within the package itself. If you want to use other data you have to download them and import them in your R session (use st_read() from the sf package to import geo data).
      The best source for Municipalities is the ADMINEXPRESS product from IGN. https://geoservices.ign.fr/documentation/diffusion/telechargement-donnees-libres.html#admin-express
      You may have to import the whole set of municipalities (36600) and then select the ones from Gironde.

  4. Hi! many thanks for this wonderful package! referent the first inset figure, when I tried to replicate the chunk I noted some maybe small bugs.
    In: mf_map(mtq_inset, add = T, col = “tomato”), the x must be mtq_target
    the same in: mf_init(mtq_inset),
    and: mf_label(mtq_inset, var = “LIBGEO”, col = “black”)
    with those modifications I succedd to reproduce the figure.
    As a fan of the sf and stars package, I am very delighted with this and I will continue to explore the options. Regards

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Ce site utilise Akismet pour réduire les indésirables. En savoir plus sur comment les données de vos commentaires sont utilisées.