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() 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…).


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:


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 |>) :


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.


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.

4 réflexions sur « The mapsf package »

  1. 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.
      You may have to import the whole set of municipalities (36600) and then select the ones from Gironde.

  2. 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.