[This article was first published on modTools, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Maps of continents, mainlands and islands can be useful, for example, for selecting areas — and then cropping or masking variables — for modelling a species’ distribution. Here’s a way to obtain such maps using the ‘geodata’ and ‘terra’ R packages:
# load required packages: library(terra) library(geodata) # import a world countries map: countries <- world(resolution = 5, path = "maps") # you may choose a smaller (more detailed) resolution for the polygon borders, and a different folder path to save the imported map head(countries) # import a table with country codes and continents: cntry_codes <- country_codes() head(cntry_codes) # add this table to the countries map attributes: head(countries) head(cntry_codes[ , 1:4]) countries <- merge(countries, cntry_codes, by.x = "GID_0", by.y = "ISO3", all.x = TRUE) head(countries) # plot the countries map coloured according to "continent": plot(countries, "continent", lwd = 0.2, main = "Countries by continent")
# dissolve (aggregate) countries into a continents map: continents <- aggregate(countries, by = "continent") values(continents) plot(continents, "continent", lwd = 0.2)
# note that each continent (colour) is a multi-part polygon including mainland and islands - see also: plot(continents[1, ]) # disaggregate continent polygons, to then separate islands and mainlands: continents <- disagg(continents) # get a map of just the continent mainlands (largest polygons): unique(continents$continent) largest <- (order(expanse(continents), decreasing = TRUE))[1:length(unique(continents$continent))] mainlands <- continents[largest, ] plot(mainlands, "continent", lwd = 0.2, main = "Continent mainlands")
# get a map of just the islands (i.e. polygons except mainlands): islands <- erase(continents, mainlands) plot(islands, "continent", lwd = 0.2, main = "World islands")
# you can then crop and mask a raster map to given islands or continents, e.g.: elevation <- elevation_global(res = 10, path = "maps") # you may choose a smaller (more detailed) resolution or pixel size elev_afr_mainland <- crop(elevation, subset(mainlands, mainlands$continent == "Africa"), mask = TRUE) plot(elev_afr_mainland, main = "Elevation in mainland Africa")
You can also use the geodata::gadm()
function to download polygons for particular countries (instead of the whole world), and then apply similar procedures to separate islands from mainlands.
To leave a comment for the author, please follow the link and comment on their blog: modTools.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.