This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# load libraries | |
library(sf) | |
library(cartography) | |
## Import | |
# get the path to the shapefile embedded in sf package | |
shapepath <- system.file("shape/nc.shp", package="sf") | |
# import the shapefile | |
nc <- st_read(shapepath) | |
# change the projection of the map | |
nc <- st_transform(nc, 3857) | |
# get a figure ratio adapted to the basemap | |
figdim <- getFigDim(x = nc, width = 800, mar = c(0,0,1.2,0), res = 100) | |
## Proportionnal symbols map | |
# png(filename = "map1.png", width = figdim[1], height = figdim[2], | |
# res = 100) | |
# set figure margins | |
par(mar = c(0,0,1.2,0)) | |
# display the North Carolina counties | |
plot(st_geometry(nc), col = "khaki4", border = "khaki3", | |
bg = "lemonchiffon") | |
# add the number of birth | |
propSymbolsLayer(x = nc,var = "BIR74", inches = 0.2, | |
col = "lightslategrey", | |
legend.title.txt = "N. of live births", | |
legend.pos = 'topleft') | |
# add a title and layout | |
layoutLayer(title = "Births in North Carolina, 1974-1978", | |
theme = "sand.pal", frame =F, north = TRUE, sources = "", | |
author = "North Carolina SIDS data set", scale = 50) | |
# dev.off() | |
## Proportionnal symbols map with a custom legend | |
# png(filename = "map2.png", width = figdim[1], height = figdim[2], | |
# res = 100) | |
# set figure margins | |
par(mar = c(0,0,1.2,0)) | |
# display the North Carolina counties | |
plot(st_geometry(nc), col = "khaki4", border = "khaki3", | |
bg = "lemonchiffon") | |
# add the number of birth (with no legend) | |
propSymbolsLayer(x = nc,var = "BIR74", inches = 0.2, | |
col = "lightslategrey", | |
legend.title.txt = "N. of live births", | |
legend.pos = 'n') | |
# add a custom legend | |
legendCirclesSymbols(pos = c( -9205821, 4062698), | |
title.txt = "N. of live births", | |
var = c(250, 4000, 10000, 21590), | |
inches = 0.2, | |
col = "lightslategrey") | |
# add a title and layout | |
layoutLayer(title = "Births in North Carolina, 1974-1978", | |
theme = "sand.pal", frame =F, north = TRUE, sources = "", | |
author = "North Carolina SIDS data set", scale = 50) | |
# dev.off() | |
## Proportional and Choropleth Symbols Layer | |
# png(filename = "map3.png", width = figdim[1], height = figdim[2], | |
# res = 100) | |
# compute the sudden infant deaths per 1000 births | |
nc$share <- 100000 * nc$SID74 / nc$BIR74 | |
# quantization breaks of the rate | |
bks <- getBreaks(v = nc$share, method = "quantile", nclass = 5) | |
# correct the breaks to use the global rate as limit of class | |
global_rate <- sum(nc$SID74) * 100000 / sum(nc$BIR74) | |
bks[4] <- global_rate | |
# get a color palette | |
cols <- carto.pal(pal1 = "green.pal", n1 = 3, pal2 = "wine.pal", n2 = 2) | |
# set figure margins | |
par(mar = c(0,0,1.2,0)) | |
# display the North Carolina counties | |
plot(st_geometry(nc), col = "khaki4", border = "khaki3", | |
bg = "lemonchiffon") | |
# display circles | |
propSymbolsChoroLayer(x = nc,var = "BIR74", inches = 0.2, | |
var2 = "share", breaks = bks, col = cols, | |
legend.var.title.txt = "N. of live births", | |
legend.var.pos = 'bottomleft', | |
legend.var2.values.rnd = 0, | |
legend.var2.pos = "topleft", | |
legend.var2.title.txt = "Sudden infant death syndrome rate*") | |
# add a title and layout | |
layoutLayer(title = "Sudden Infant Death Syndrome in North Carolina, 1974-1978", | |
sources = "", north = TRUE, scale = 50, | |
theme = "sand.pal", frame = FALSE, | |
author = "*per 100,000 live births. Source: North Carolina SIDS data set") | |
# dev.off() | |
# get the path to the shapefile embedded in cartography package | |
shapepath <- system.file("shape/martinique.shp", package="cartography") | |
# import the shapefile | |
mtq <- st_read(shapepath) | |
# get a figure ratio adapted to the basemap | |
figdim <- getFigDim(x = mtq, height = 800, mar = c(0,0,1.2,0), res = 100) | |
## Label map (default) | |
# png(filename = "map4.png", width = figdim[1], height = figdim[2], | |
# res = 100) | |
# set figure margins | |
par(mar = c(0,0,1.2,0)) | |
# display Martinique communes | |
plot(st_geometry(mtq), col = "khaki4", border = "khaki3", | |
bg = "aliceblue") | |
# display labels (default) | |
labelLayer(x = mtq, txt = "LIBGEO", cex = 1.2) | |
# add a title and layout | |
layoutLayer(title = "Martinique Communes", | |
sources = "", north = TRUE, scale = 5, | |
theme = "sand.pal", frame = FALSE, | |
author = "INSEE, 2016") | |
# dev.off() | |
## Label map (custom) | |
# png(filename = "map5.png", width = figdim[1], height = figdim[2], | |
# res = 100) | |
# set figure margins | |
par(mar = c(0,0,1.2,0)) | |
# display Martinique communes | |
plot(st_geometry(mtq), col = "khaki4", border = "khaki3", | |
bg = "aliceblue") | |
# display labels (default) | |
labelLayer(x = mtq, txt = "LIBGEO", cex = 1.2, | |
overlap = FALSE, show.lines = FALSE, | |
halo = TRUE, bg = "white", r = 0.15) | |
# add a title and layout | |
layoutLayer(title = "Martinique Communes", | |
sources = "", north = TRUE, scale = 5, | |
theme = "sand.pal", frame = FALSE, | |
author = "INSEE, 2016") | |
# dev.off() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment