library(linemap)
<- terra::rast(system.file("tif/elevation.tif", package = "linemap"))
r <- par(mar = c(0,0,0,0), bg = "grey10")
opar linemap(r)
par(opar)
linemap
update
linemap
The linemap
package (Giraud 2023) contains just one function: linemap()
. linemap()
displays a map made of lines using raster or gridded data.
In the previous version of the package, linemap()
only accepted as input a data.frame
of gridded data (in its long form: lon, lat, value). That was highly impractical.
The main change introduced by this release is the ability to use a raster as input (aSpatRaster
from terra
).
In this example we use the sample dataset shipped with the package. elevation.tif is an elevation raster of Grenoble and the surrounding area (from SRTM).
We can use any kind of raster data. The following example uses gridded population data.
Code
# data preparation
library(terra)
library(sf)
# Download
<- tempfile()
tempzip <- tempdir()
tempfolder <- paste0("http://cidportal.jrc.ec.europa.eu/ftp/jrc-opendata/GHSL/",
data_url "GHS_POP_GPW4_GLOBE_R2015A/GHS_POP_GPW42015_GLOBE_R2015A_54009_1k/",
"V1-0/GHS_POP_GPW42015_GLOBE_R2015A_54009_1k_v1_0.zip")
download.file(data_url, tempzip)
unzip(tempzip, exdir = tempfolder)
# Import
<- rast(paste0(tempfolder,
pop2015 "/GHS_POP_GPW42015_GLOBE_R2015A_54009_1k_v1_0/",
"GHS_POP_GPW42015_GLOBE_R2015A_54009_1k_v1_0.tif"))
# Mask raster
<- st_as_sf(data.frame(x=425483.8, y=5608290),
center coords=(c("x","y")),
crs = st_crs(pop2015))
<- st_buffer(center, dist = 800000)
center <- crop(pop2015, st_bbox(center)[c(1,3,2,4)])
ras # Smooth values
<- ras
model_r res(model_r) <- c(5000, 10000)
<- resample(ras, model_r, method = "sum")
ras_res <- mask(ras_res, center) r
library(mapsf)
mf_theme(mar = c(0,0,0,0))
mf_map(center, col = "black", border = "black", lwd = 4)
linemap(r, k = .5, col = "black", border = "white", lwd = 1, add= TRUE)
mf_title(txt = "Population", inner = TRUE, bg = "#f7f7f7",
fg = "#333333", line = 2, cex = 1.5)
mf_credits(paste0("T. Giraud, 2023\n",
"Data : European Commission, Joint Research Centre (JRC); ",
"Columbia University, CIESIN (2015): GHS population grid, ",
"derived from GPW4."))
linemap
repository on GitHub: github.com/riatelab/linemap
References
Giraud, Timothée. 2023. linemap: Line Maps. https://CRAN.R-project.org/package=linemap.