Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
## Figure 1
#download the dev version of cartography
devtools::install_github("riatelab/cartography")
library(sf)
library(cartography)
mtq <- st_read(system.file("shape/martinique.shp", package="cartography"))
mt1 <- mtq[3,]
mt2 <- getPencilLayer(mt1, size = 1000, lefthanded = T, buffer = 2000)
png(filename = "fig1.png", width = 474, height = 132, res = 100)
par(mfrow=c(1,3), mar = c(0,0,0,0))
plot(mt1$geometry, col = "lightblue")
plot(c(0,1),c(1,1), col = NA, axes = F, xlab="", ylab="")
arrows(0,1,1,1, lwd = 3)
text(0.5, 1.2, labels = "getPencilLayer()", cex = 1.5)
plot(mt2$geometry, col = "lightblue", lwd = 0.5)
dev.off()
## Figure 2
png(filename = "fig2.png", width = 474, height = 553, res = 100)
par(mfrow=c(1,1), mar = c(0,0,0,0))
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)
dev.off()
## Figure 3
# transform the POLYGON layer in a MULTILINESTRING layer
mtq_pencil <- getPencilLayer(x = mtq)
png(filename = "fig3.png", width = 474, height = 553, res = 100)
par(mfrow=c(1,1), mar = c(0,0,0,0))
# 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)
dev.off()
## Figure 4
# Share of farmers in the active population
mtq$shareCS1 <- 100 * mtq$C13_CS1/mtq$C13_POP
# transform communes polygons to (multi)linestrings
mtq_pencil <- getPencilLayer(mtq, size = 500, lefthanded = FALSE)
png(filename = "fig4.png", width = 474, height = 500, res = 100)
par(mfrow=c(1,1), mar = c(0,0,1.2,0))
# 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")
dev.off()
## Figure 5
library(sf)
library(cartography)
library(png)
# import background image
if (!file.exists("background.png")) {
githubURL <- "https://raw.githubusercontent.com/gadenbuie/ggpomological/master/inst/images/pomological_background.png"
download.file(githubURL, "background.png")
}
img <- readPNG("background.png")
# import Communes of Martinique (sf dataframe within cartography package)
mtq <- st_read(system.file("shape/martinique.shp", package="cartography"))
# compute the compound annual growth rate
mtq$cagr <- (((mtq$P13_POP / mtq$P08_POP)^(1/4)) - 1) * 100
# create a color palette
cols <- carto.pal(pal1 = "blue.pal", n1 = 3, pal2 = "wine.pal", n2 = 2)
# transform communes polygons to (multi)linestrings
mtq_pencil <- getPencilLayer(mtq, size = 500, lefthanded = F)
# export the plot
png("fig5.png", width = 474, height = 577, res = 130, bg=NA)
# set figure margins and font family
par(family="Nanum Pen", 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 compound annual growth rate
choroLayer(x = mtq_pencil, var = "cagr", breaks = c(-6.14,-2,-1,0,1,2),
col = cols, lwd = .7, add = TRUE, legend.pos = "topright",
legend.values.rnd = 2, border = "grey",
legend.title.txt = "Compound annual growth rate")
# communes boundaries
plot(st_geometry(mtq), lwd = 0.5, border = "grey20", add = TRUE, lty = 3)
# labels for a few communes
labelLayer(x = mtq[mtq$STATUT != "Commune simple",], 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 = "Demographic Trends in Martinique, 2008-2013",
sources = "INSEE, 2016",
author = "Timothee Giraud, 2018",
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
You can’t perform that action at this time.