# 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
devtools::install_github("riatelab/cartography")
library(cartography)
library(sf)
# import a vector layer (here a shapefile)
# 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
`````` `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. Rasyid Ridha dit :

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

1. Timothée Giraud dit :

Hello,
I don’t know what you are actually doing, so I have no idea why this happens neither ;-)…
Can you show me a reproducible example ? perhaps using github issues (https://github.com/riatelab/cartography/issues).

Ce site utilise Akismet pour réduire les indésirables. En savoir plus sur comment les données de vos commentaires sont utilisées.
{"ft_lang_label":"__label__en","ft_lang_prob":0.63491553,"math_prob":0.81010866,"size":2459,"snap":"2019-51-2020-05","text_gpt3_token_len":658,"char_repetition_ratio":0.09490835,"word_repetition_ratio":0.016949153,"special_character_ratio":0.25335503,"punctuation_ratio":0.14084508,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9559617,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],"im_url_duplicate_count":[null,1,null,5,null,5,null,5,null,5,null,5,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-01-20T14:17:39Z\",\"WARC-Record-ID\":\"<urn:uuid:f842e107-278c-477a-bf0a-1d74acff7c92>\",\"Content-Length\":\"60172\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:a42bb28e-8fe4-4c78-9d55-384a91a5dcc8>\",\"WARC-Concurrent-To\":\"<urn:uuid:3a110fd3-3204-4cba-b0f0-8a36fc71e54b>\",\"WARC-IP-Address\":\"134.158.39.133\",\"WARC-Target-URI\":\"https://rgeomatic.hypotheses.org/1333\",\"WARC-Payload-Digest\":\"sha1:JMP6OG4HCO3RHKKFE57EE7A2XDLWVL3P\",\"WARC-Block-Digest\":\"sha1:H5K4NHT6BSXYM3KGTBSMWRPRSD76ICS4\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-05/CC-MAIN-2020-05_segments_1579250598800.30_warc_CC-MAIN-20200120135447-20200120164447-00424.warc.gz\"}"}