Create and integrate maps in your R workflow. This package helps to design cartographic representations such as proportional symbols, choropleth, typology, flows or discontinuities maps. It also offers several features that improve the graphic presentation of maps, for instance, map palettes, layout elements (scale, north arrow, title...), labels or legends. See Giraud and Lambert (2017)
This package helps to design cartographic representations such as proportional symbols, choropleth, typology, flows or discontinuities maps. It also offers several features that improve the graphic presentation of maps, for instance, map palettes, layout elements (scale, north arrow, title...), labels or legends.
The cheat sheet displays a quick overview of
cartography's main features.
contains commented scripts on how to build various types of maps with
The following script creates a map of symbols that are proportional to values of a first variable and colored to reflect the classification of a second variable.
library(sf)library(cartography)# path to the geopackage file embedded in cartographypath_to_file <- system.file("gpkg/mtq.gpkg", package="cartography")# import to an sf objectmtq <- st_read(dsn = path_to_file, quiet = TRUE)########## Draft Map# Plot the municipalitiesplot(st_geometry(mtq))# Plot symbols with choropleth coloration (population & median income)propSymbolsChoroLayer(x = mtq, var = "POP", var2 = "MED")# Add a layouttitle(main = "Population & Wealth in Martinique, 2015",sub = "Sources: Insee and IGN - 2018")
########## Final Map# Set figure marginsopar <- par(mar = c(0,0,1.2,0))# Plot the municipalitiesplot(st_geometry(mtq), col="darkseagreen3", border="darkseagreen4",bg = "lightblue1", lwd = 0.5)# Plot symbols with choropleth colorationpropSymbolsChoroLayer(x = mtq, var = "POP", inches = 0.4, border = "grey50",lwd = 1, legend.var.pos = "topright",legend.var.title.txt = "Population",var2 = "MED", method = "equal", nclass = 4,col = carto.pal(pal1 = "sand.pal", n1 = 4),legend.var2.values.rnd = -2, legend.var2.pos = "left",legend.var2.title.txt = "Median Income\n(in euros)")# Plot a layoutlayoutLayer(title="Population & Wealth in Martinique, 2015",author = "cartography 2.1.3",sources = "Sources: Insee and IGN - 2018",scale = 5, tabtitle = TRUE, frame = FALSE)# Plot a north arrownorth(pos = "topleft")# restore graphics parameterspar(opar)
Up to version 1.4.2 cartography was mainly based on sp and and rgeos for its spatial data management and geoprocessing operations. These dependencies are 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.
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.