Create and integrate maps in your R workflow. This package allows various cartographic representations such as proportional symbols, choropleth, typology, flows or discontinuities maps. It also offers several features enhancing the graphic presentation of maps like cartographic palettes, layout elements (scale, north arrow, title...), labels, legends or access to some cartographic APIs. See Giraud and Lambert (2017)
This package allows various cartographic representations such as proportional symbols, chroropleth, typology, flows or discontinuities maps. It also offers several features enhancing the graphic presentation of maps: cartographic palettes, layout elements (scale, north arrow, title...), labels, legends or access to some cartographic APIs.
cartography uses R base graphics to map spatial information.
Up to version 1.4.2
cartography was mainly based on
rgeos for its spatial data management and geoprocessing operations. These dependencies have been as much as possible replaced by
sf functions since version 2.0.0.
Most functions are kept unchanged except for the addition of an
x argument used to take
sf objects as inputs.
contains commented scripts on how to build various types of maps with
vignette(topic = "cartography")
Giraud, T. and Lambert, N. (2017). “Reproducible Cartography.” In Peterson MP (ed.), Advances in Cartography and GIScience. ICACI 2017. Lecture Notes in Geoinformation and Cartography., pp. 173-183. doi: 10.1007/978-3-319-57336-6_13. (EN)
New version of the cartography package (EN)
Demo codes in the R graph Gallery (EN)
Create and integrate maps in your R workflow with the cartography package (EN)
SF & cartography V2.0.0 (FR)
De superbes cartes thématiques... (FR)
Le package cartography a un an (FR)
Cartographie avec R : le package cartography (FR)
R pour les cartographes (FR)
Comment faire un carton avec R? (FR - How to build inset maps)
Tutoriel - Cartographie avec R (FR)
Cartographie et traitement de l’information géographique avec R (FR)
R pour les cartographes : le package cartography (FR)
You can access the code used to create the cartographic mix here.
The following script creates a map of symbols that are proportional to values of a first variable and colored to reflect the discretization of a second variable.
library(cartography)# Import a shapefile // this one is distributed within the packagemtq <- st_read(system.file("shape/martinique.shp", package="cartography"))# Share of farmers in the active populationmtq$shareCS1 <- 100 * mtq$C13_CS1/mtq$C13_POP# Set a custom color palettecols <- carto.pal(pal1 = "wine.pal", n1 = 6)# set plot marginsopar <- par(mar = c(0,0,1.2,0))# Plot the communesplot(st_geometry(mtq), col = "#5F799C", border = "white",bg = "#A6CAE0", lwd = 0.5, add = FALSE)# Plot symbols with choropleth colorationpropSymbolsChoroLayer(x = mtq, # sf objectvar = "C13_POP", # field used to plot the symbols sizesvar2 = "shareCS1", # field used to plot the colorscol = cols, # symbols colorsinches = 0.4, # radius of the largest circlemethod = "quantile", # discretization method (?getBreaks)border = "grey50", # color of circle borderslwd = 1, # width of the circle borderslegend.var.pos = "topright", # position of the first legendlegend.var2.pos = "left", # position of the second legendlegend.var2.title.txt ="Share of \nthe population\nworking in\nagriculture (%)",legend.var.title.txt = "Population aged\n15 and over",legend.var.style = "c") # legend style# Add a layoutlayoutLayer(title="Farmers in Martinique, 2013", # title of the mapscale = 5, # size of the scale barnorth = TRUE, # north arrowcol = "white",coltitle = "black",author = "cartography 2.0.0",sources = "INSEE, 2016",frame = TRUE)# restore graphics parameterspar(opar)
getBorders, getOuterBorders: an x argument is added in first position, x is added to take sf objects as inputs, if x is used then spdf is not used. spdfid is replaced by id. An sf object (MULTILINESTRING) of borders is returned.
discLayer: spdf, spdfid1, spdfid2 and dfid are replaced by x (an sf object as outputed by getBorders).
getGridLayer: getGridLayer replaces getGridLayer + getGridData, x argument replaces spdf and can take an sf object or an sp object. var is added to enter the name(s) of numeric field(s) to compute into the grid. cellsize refers to the targeted area of the cells. The function returns an sf object
getLinkLayer: spdf, spdf2, df, spdfid, spdf2id, dfids and dfide arguments are replaced by x, xid, df and dfid. x can be either a Spatial*DataFrame or an sf object. The function returns an sf LINESTRING instead of a SpatialLinesDataFrame.
propLinkLayer, gradLinkLayer, gradLinkTypoLayer: spdf, df, spdfid, spdfids, spdfide, dfid, dfids and dfide arguments are replaced by x, df, xid and dfid. x must be an sf object (as outputed by getLinkLayer).
legend* functions have been enhanced to be more customizable. It is now possible to lay legend on specified x & y.
propSymbolsLayer: breakval and col2 arguments are defunct, use propSymbolsTypoLayer if needed.
choroLayer, typoLayer, propSymbolsLayer, propSymbolsChoroLayer, propSymbolsTypoLayer, dotDensityLayer, getTiles, getFigDim, labelLayer, smoothLayer: an x argument is added in first position of all these functions. x is added to take sf objects as inputs, if x is used then spdf, df, spdfid, id are not used.
labelLayer: possibility to add a colored halo around the labels, option to have non-overlapping labels.
layoutLayer: the extent argument can take an sf object.
Each function has an example based on sf object.
possibility to use text labels in legendChoro