New version of the cartography package

A new version of the cartography package (v2.0.1) has arrived on CRAN.

cartography 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 like cartographic palettes, layout elements (scale, north arrow, title…), labels, legends or access to some cartographic APIs.

Up to version 1.4.2 cartography was mainly based on sp and 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.
See the NEWS file for the full list of changes and see sf README in case of installation problems with sf.

Use Case Examples

Here are some use case examples of the package. The first data set (North Carolina SIDS data set) is embedded in the sf package and the second one is embedded in cartography but of course, one can use any geospatial vector data file. The following scrips are also hosted on this gist.

Proportional Symbols

# load libraries
library(sf)
library(cartography)

## Import
# get the path to the shapefile embedded in sf package
shapepath <- system.file("shape/nc.shp", package="sf")
# import the shapefile
nc <- st_read(shapepath)
# change the projection of the map
nc <- st_transform(nc, 3857)
# get a figure ratio adapted to the basemap
figdim <- getFigDim(x = nc, width = 800, mar = c(0,0,1.2,0), res = 100)

## Proportionnal symbols map
# png(filename = "map1.png", width = figdim[1], height = figdim[2], 
#     res = 100)
# set figure margins
par(mar = c(0,0,1.2,0))
# display the North Carolina counties
plot(st_geometry(nc), col = "khaki4", border = "khaki3", 
     bg = "lemonchiffon")
# add the number of birth
propSymbolsLayer(x = nc,var = "BIR74", inches = 0.2, 
                 col = "lightslategrey",
                 legend.title.txt = "N. of live births", 
                 legend.pos = 'topleft')
# add a title and layout
layoutLayer(title = "Births in North Carolina, 1974-1978",  
            theme = "sand.pal", frame =F, north = TRUE, sources = "",
            author = "North Carolina SIDS data set", scale = 50)
# dev.off()
click to enlarge

It is now possible to define a specific location for the legend (x,y, coordinates on the map) an to specify reference values for symbols:

## Proportionnal symbols map with a custom legend
# png(filename = "map2.png", width = figdim[1], height = figdim[2], 
#     res = 100)
# set figure margins
par(mar = c(0,0,1.2,0))
# display the North Carolina counties
plot(st_geometry(nc), col = "khaki4", border = "khaki3", 
     bg = "lemonchiffon")
# add the number of birth (with no legend)
propSymbolsLayer(x = nc,var = "BIR74", inches = 0.2, 
                 col = "lightslategrey",
                 legend.title.txt = "N. of live births", 
                 legend.pos = 'n')
# add a custom legend
legendCirclesSymbols(pos = c(-9205821, 4062698), 
                     title.txt = "N. of live births", 
                     var = c(250, 4000, 10000, 21590), 
                     inches = 0.2, 
                     col = "lightslategrey")
# add a title and layout
layoutLayer(title = "Births in North Carolina, 1974-1978", 
            theme = "sand.pal", frame =F, north = TRUE, sources = "", 
            author = "North Carolina SIDS data set", scale = 50)
# dev.off()
click to enlarge

Proportional and Choropleth Symbols

## Proportional and Choropleth Symbols Layer
# png(filename = "map3.png", width = figdim[1], height = figdim[2],
#     res = 100)
# compute the sudden infant deaths per 1000 births
nc$share <- 100000 * nc$SID74 / nc$BIR74
# quantization breaks of the rate
bks <- getBreaks(v = nc$share, method = "quantile", nclass = 5)
# correct the breaks to use the global rate as limit of class 
global_rate <- sum(nc$SID74) * 100000 / sum(nc$BIR74)
bks[4] <- global_rate
# get a color palette
cols <- carto.pal(pal1 = "green.pal", n1 = 3, pal2 = "wine.pal", n2 = 2)
# set figure margins
par(mar = c(0,0,1.2,0))
# display the North Carolina counties
plot(st_geometry(nc), col = "khaki4", border = "khaki3", 
     bg = "lemonchiffon")
# display circles
propSymbolsChoroLayer(x = nc,var = "BIR74", inches = 0.2,
                      var2 = "share", breaks = bks, col = cols, 
                      legend.var.title.txt = "N. of live births", 
                      legend.var.pos = 'bottomleft', 
                      legend.var2.values.rnd = 0,
                      legend.var2.pos = "topleft", 
                      legend.var2.title.txt = "Sudden infant death syndrome rate*")
# add a title and layout
layoutLayer(title = "Sudden Infant Death Syndrome in North Carolina, 1974-1978", 
            sources = "", north = TRUE, scale = 50,
            theme = "sand.pal", frame = FALSE, 
            author = "*per 100,000 live births. Source: North Carolina SIDS data set")
# dev.off()
click to enlarge

Labels

# get the path to the shapefile embedded in cartography package
shapepath <- system.file("shape/martinique.shp", package="cartography")
# import the shapefile
mtq <- st_read(shapepath)
# get a figure ratio adapted to the basemap
figdim <- getFigDim(x = mtq, height = 800, mar = c(0,0,1.2,0), res = 100)

## Label map (default)
# png(filename = "map4.png", width = figdim[1], height = figdim[2], 
#     res = 100)
# set figure margins
par(mar = c(0,0,1.2,0))
# display Martinique communes
plot(st_geometry(mtq), col = "khaki4", border = "khaki3", 
     bg = "aliceblue")
# display labels (default)
labelLayer(x = mtq, txt = "LIBGEO", cex = 1.2)
# add a title and layout
layoutLayer(title = "Martinique Communes", 
            sources = "", north = TRUE, scale = 5,
            theme = "sand.pal", frame = FALSE, 
            author = "INSEE, 2016")
# dev.off()
click to enlarge

It is now possible to add a halo around labels and to avoid overlapping:

## Label map (custom)
# png(filename = "map5.png", width = figdim[1], height = figdim[2], 
#     res = 100)
# set figure margins
par(mar = c(0,0,1.2,0))
# display Martinique communes
plot(st_geometry(mtq), col = "khaki4", border = "khaki3", 
     bg = "aliceblue")
# display labels (default)
labelLayer(x = mtq, txt = "LIBGEO", cex = 1.2,
           overlap = FALSE, show.lines = FALSE, 
           halo = TRUE, bg = "white", r = 0.15)
# add a title and layout
layoutLayer(title = "Martinique Communes", 
            sources = "", north = TRUE, scale = 5,
            theme = "sand.pal", frame = FALSE, 
            author = "INSEE, 2016")
# dev.off()
click to enlarge

Cartographic Mix


Script of the cartographic mix

Questions? Issues?

The stable release of the package is on CRAN and the development version is hosted on GitHub.
Feel free to submit issues, report bugs or ask questions here.


OpenEdition vous propose de citer ce billet de la manière suivante :
Timothée Giraud (27 septembre 2017). New version of the cartography package. R Géomatique. Consulté le 10 octobre 2024 à l’adresse https://doi.org/10.58079/tp48


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.