Colored Pencil Maps with R

This post shows how to build a colored pencil map with R.
The cartography package (in its development version) has gained a getPencilLayer() function that transforms a POLYGON or MULTIPOLYGON layer into a MULTILINESTRING layer mimicking a pencil drawing pattern.


The following code details how to use the function:

#download the dev version of cartography
# import a vector layer (here a shapefile)
mtq <- st_read(system.file("shape/martinique.shp", package="cartography"))
# display this POLYGON layer
plot(st_geometry(mtq), col = 1:8)


# transform the POLYGON layer in a MULTILINESTRING layer
mtq_pencil <- getPencilLayer(x = mtq)
# display this MULTILINESTRING layer
plot(st_geometry(mtq_pencil), col = 1:8)
# and a add the original borders
plot(st_geometry(mtq), col = NA, add=T)

getPencilLayer() produces a MULTILINESTRING sf data frame that keeps original data, hence the possibility to use this layer in mapping functions (such as choroLayer() or typoLayer()):

# Share of farmers in the active population
mtq$shareCS1 <- 100 * mtq$C13_CS1/mtq$C13_POP
# transform communes polygons to multilinestrings
mtq_pencil <- getPencilLayer(mtq, size = 500, lefthanded = FALSE)
# plot the Share of farmers 
choroLayer(x = mtq_pencil, var = "shareCS1", 
           method = "quantile", nclass = 4,
           lwd = .7, 
           legend.values.rnd = 2, legend.pos = "topright",
           legend.title.txt = "Share of \nthe population\nworking in\nagriculture (%)")
# communes boundaries
plot(st_geometry(mtq), lwd = 0.5, add = TRUE)
# map layout
layoutLayer(title="Farmers in Martinique, 2013", 
            scale = 5, col = "white", coltitle = "black",
            author = "cartography_2.1.1.9000",  
            sources = "INSEE, 2016")

Inspired by some ideas and tips exposed in this post (Draw maps like paintings), I used a handwritten font and a background image to produce the following map:

The code used to produce all figures in this post is available here.

5 réflexions sur « Colored Pencil Maps with R »

  1. Hi
    I am trying in my sf data frame and get this error

    Error in data.frame(x[-all_sfc_columns], row.names = row.names, stringsAsFactors = stringsAsFactors) :
    row names supplied are of the wrong length

    I have no idea why this happens

Laisser un commentaire

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

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