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)
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.
OpenEdition vous propose de citer ce billet de la manière suivante :
Timothée Giraud (23 mai 2018). Colored Pencil Maps with R. R Géomatique. Consulté le 13 janvier 2025 à l’adresse https://doi.org/10.58079/tp4c
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
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).