Last active
July 5, 2018 12:01
-
-
Save rCarto/3d5320d769debabe8e9f4dad5f82386e to your computer and use it in GitHub Desktop.
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) | |
library(cartogram) | |
## Import | |
# get the path to the shapefile embedded in sf package | |
shapepath <- system.file("shape/nc.shp", | |
package="sf") | |
# import the shapefile | |
nc <- sf::st_read(shapepath) | |
# change the projection of the map | |
nc <- sf::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) | |
# data preparation | |
# 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) | |
## Choropleth layer | |
png(filename = "map1.png", width = figdim[1], height = figdim[2], | |
res = 100) | |
# set figure margins and background color | |
par(mar = c(0,0,1.2,0), bg = "lemonchiffon") | |
# display the sudden infant deaths per 1000 births | |
choroLayer(x = nc,var = "share", breaks = bks, col = cols, | |
border = "khaki", lwd = 0.5, | |
legend.title.txt = "Sudden infant death syndrome rate*", | |
legend.pos = 'topleft', legend.values.rnd = 0) | |
# add a title and layout | |
layoutLayer(title = "Sudden Infant Death Syndrome in North Carolina, 1974-1978", | |
sources = "", north = TRUE, scale = 50,tabtitle = TRUE, | |
theme = "sand.pal", frame = FALSE, | |
author = "*per 100,000 live births. Source: North Carolina SIDS data set") | |
dev.off() | |
## Choropleth layer | |
png(filename = "map2.png", width = figdim[1], height = figdim[2], | |
res = 100) | |
## Proportional and Choropleth Symbols Layer | |
# set figure margins | |
par(mar = c(0,0,1.2,0), bg = "lemonchiffon") | |
# display the North Carolina counties | |
plot(st_geometry(nc), col = "khaki4", border = "khaki3") | |
# display circles | |
propSymbolsChoroLayer(x = nc,var = "BIR74", inches = 0.35, | |
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,tabtitle = TRUE, | |
theme = "sand.pal", frame = FALSE, | |
author = "*per 100,000 live births. Source: North Carolina SIDS data set") | |
dev.off() | |
# Continuous cartogram | |
nc_cont <- cartogram_cont(nc, "BIR74") | |
## Choropleth layer | |
png(filename = "map3.png", width = figdim[1], height = figdim[2], | |
res = 100) | |
# set figure margins and background color | |
par(mar = c(0,0,1.2,0), bg = "lemonchiffon") | |
# display the sudden infant deaths per 1000 births | |
choroLayer(x = nc_cont, var = "share", breaks = bks, col = cols, | |
border = "khaki", lwd = 0.5, | |
legend.title.txt = "Sudden infant death syndrome rate*", | |
legend.pos = 'topleft', legend.values.rnd = 0) | |
text(x=-9405313, y=4082976, labels = "Counties sizes are proportional to the number\nof live births between 1974 and 1978", | |
adj = 0, cex = 0.8) | |
# add a title and layout | |
layoutLayer(title = "Sudden Infant Death Syndrome in North Carolina, 1974-1978", | |
sources = "", north = FALSE, scale = NULL,tabtitle = TRUE, | |
theme = "sand.pal", frame = FALSE, | |
author = "*per 100,000 live births. Source: North Carolina SIDS data set") | |
dev.off() | |
nc_ncont <- cartogram_ncont(nc, "BIR74") | |
## Choropleth layer | |
png(filename = "map4.png", width = figdim[1], height = figdim[2], | |
res = 100) | |
# set figure margins and background color | |
par(mar = c(0,0,1.2,0), bg = "lemonchiffon") | |
# display the sudden infant deaths per 1000 births | |
choroLayer(x = nc_ncont, var = "share", breaks = bks, col = cols, | |
border = "khaki", lwd = 0.5, | |
legend.title.txt = "Sudden infant death syndrome rate*", | |
legend.pos = 'topleft', legend.values.rnd = 0) | |
plot(st_geometry(nc), col=NA, lwd = 0.5, border = "khaki", add=T) | |
text(x=-9382321, y=4082976, | |
labels = "Counties sizes are proportional to the number\nof live births between 1974 and 1978", | |
adj = 0, cex = 0.8) | |
# add a title and layout | |
layoutLayer(title = "Sudden Infant Death Syndrome in North Carolina, 1974-1978", | |
sources = "", north = FALSE, scale = NULL,tabtitle = TRUE, | |
theme = "sand.pal", frame = FALSE, | |
author = "*per 100,000 live births. Source: North Carolina SIDS data set") | |
dev.off() | |
nc_dorling <- cartogram_dorling(nc, "BIR74", k = 5) | |
## Choropleth layer | |
png(filename = "map5.png", width = figdim[1], height = figdim[2], | |
res = 100) | |
# set figure margins and background color | |
par(mar = c(0,0,1.2,0), bg = "lemonchiffon") | |
# display the sudden infant deaths per 1000 births | |
choroLayer(x = nc_dorling, var = "share", breaks = bks, col = cols, | |
border = "khaki", lwd = 0.5, | |
legend.title.txt = "Sudden infant death syndrome rate*", | |
legend.pos = 'topleft', legend.values.rnd = 0) | |
# plot(st_geometry(nc), col=NA, lwd = 0.5, border = "khaki", add=T) | |
text(x=-9602842, y=4060000, | |
labels = "Circles sizes are proportional to the number\nof live births between 1974 and 1978", | |
adj = 0, cex = 0.8) | |
# add a title and layout | |
layoutLayer(title = "Sudden Infant Death Syndrome in North Carolina, 1974-1978", | |
sources = "", north = FALSE, scale = NULL,tabtitle = TRUE, | |
theme = "sand.pal", frame = FALSE, | |
author = "*per 100,000 live births. Source: North Carolina SIDS data set") | |
dev.off() | |
w <- 1-( nc$BIR74 / max(nc$BIR74)) | |
nc_dorling <- cartogram_dorling(nc, "BIR74", | |
k = 5, m_weight = w) | |
## Choropleth layer | |
png(filename = "map6.png", width = figdim[1], height = figdim[2], | |
res = 100) | |
# set figure margins and background color | |
par(mar = c(0,0,1.2,0), bg = "lemonchiffon") | |
# display the sudden infant deaths per 1000 births | |
choroLayer(x = nc_dorling, var = "share", breaks = bks, col = cols, | |
border = "khaki", lwd = 0.5, | |
legend.title.txt = "Sudden infant death syndrome rate*", | |
legend.pos = 'topleft', legend.values.rnd = 0) | |
# plot(st_geometry(nc), col=NA, lwd = 0.5, border = "khaki", add=T) | |
text(x=-9602842, y=4060000, | |
labels = "Circles sizes are proportional to the number\nof live births between 1974 and 1978", | |
adj = 0, cex = 0.8) | |
# add a title and layout | |
layoutLayer(title = "Sudden Infant Death Syndrome in North Carolina, 1974-1978", | |
sources = "", north = FALSE, scale = NULL, | |
tabtitle = TRUE, | |
theme = "sand.pal", frame = FALSE, | |
author = "*per 100,000 live births. Source: North Carolina SIDS data set") | |
dev.off() | |
sessionInfo() | |
# R version 3.5.1 (2018-07-02) | |
# Platform: x86_64-pc-linux-gnu (64-bit) | |
# Running under: Debian GNU/Linux 9 (stretch) | |
# | |
# Matrix products: default | |
# BLAS: /usr/lib/libblas/libblas.so.3.7.0 | |
# LAPACK: /usr/lib/lapack/liblapack.so.3.7.0 | |
# | |
# locale: | |
# [1] LC_CTYPE=fr_FR.UTF-8 LC_NUMERIC=C LC_TIME=fr_FR.UTF-8 | |
# [4] LC_COLLATE=fr_FR.UTF-8 LC_MONETARY=fr_FR.UTF-8 LC_MESSAGES=fr_FR.UTF-8 | |
# [7] LC_PAPER=fr_FR.UTF-8 LC_NAME=C LC_ADDRESS=C | |
# [10] LC_TELEPHONE=C LC_MEASUREMENT=fr_FR.UTF-8 LC_IDENTIFICATION=C | |
# | |
# attached base packages: | |
# [1] stats graphics grDevices utils datasets methods base | |
# | |
# other attached packages: | |
# [1] cartogram_0.1.0 cartography_2.1.1 sf_0.6-3 | |
# | |
# loaded via a namespace (and not attached): | |
# [1] Rcpp_0.12.17 lattice_0.20-35 class_7.3-14 grid_3.5.1 spData_0.2.9.0 | |
# [6] DBI_1.0.0 magrittr_1.5 e1071_1.6-8 units_0.6-0 sp_1.3-1 | |
# [11] packcircles_0.3.2 rgdal_1.3-3 tools_3.5.1 yaml_2.1.19 compiler_3.5.1 | |
# [16] classInt_0.2-3 rgeos_0.3-28 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment