Skip to content

Instantly share code, notes, and snippets.

@rCarto
Created July 4, 2019 08:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rCarto/b77c84da0b44de4c7aead4c0235c2968 to your computer and use it in GitHub Desktop.
Save rCarto/b77c84da0b44de4c7aead4c0235c2968 to your computer and use it in GitHub Desktop.
library(sf)
library(cartography)
library(png)
# import background image
if (!file.exists("img/background.png")) {
githubURL <- "https://raw.githubusercontent.com/gadenbuie/ggpomological/master/inst/images/pomological_background.png"
download.file(githubURL, "img/background.png")
}
img <- readPNG("img/background.png")
# import Communes of Martinique (sf dataframe within cartography package)
mtq <- st_read(system.file("gpkg/mtq.gpkg", package="cartography"), quiet = TRUE)
# transform communes polygons to (multi)linestrings
mtq_pencil <- getPencilLayer(mtq, size = 500, lefthanded = F)
# export the plot
png("img/pencil.png", width = 474, height = 577, res = 99, bg=NA)
# set figure margins and font family
par( mar = c(0,0,1.2,0))
# plot the background image
plot(st_geometry(mtq), col = NA, border = NA)
rasterImage(img, par()$usr[1], par()$usr[3], par()$usr[2], par()$usr[4])
# plot the median income
choroLayer(x = mtq_pencil, var = "MED", method = "equal", nclass = 4,
col = carto.pal("green.pal", 4), lwd = .7, add = TRUE,
legend.pos = "topright",
legend.values.rnd = -2,
legend.title.txt = "Median Income (in euros)")
# communes boundaries
plot(st_geometry(mtq), lwd = 0.5, border = "grey20", add = TRUE, lty = 3)
# labels for a few communes
mtq$LIBGEO <- iconv(mtq$LIBGEO, to="ASCII//TRANSLIT//IGNORE")
labelLayer(x = mtq[mtq$STATUS != "Simple municipality",],
txt = "LIBGEO",
cex = 0.9, halo = T, r = 0.15)
# scale bar
barscale(size = 5,cex = 0.8, lwd = 1)
# north arrow
north(pos = "topleft")
# title, source, author
layoutLayer(title = "Wealth in Martinique, 2015",
sources = "INSEE, 2016",
author = "Timothee Giraud, 2019",
scale = NULL, tabtitle = T, north = FALSE, frame = FALSE)
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment