Instantly share code, notes, and snippets.

@rCarto /zoom.R
Last active Feb 9, 2018

Embed
What would you like to do?
# load libraries
library(sf)
library(cartography)
library(rnaturalearth)
# import world countries and coastlines from Natural Earth
country <- ne_countries(scale = 50, returnclass = "sf",
type="sovereignty")
coastline <- ne_coastline(scale = 50, returnclass = "sf")
# Change the projection to Natural Earth II projection
# (http://www.shadedrelief.com/NE2_proj/)
country <- st_transform(country, "+proj=natearth +wktext")
coastline <- st_transform(coastline, "+proj=natearth +wktext")
# Define a palette of pastel colors
cols <- carto.pal("pastel.pal",20)
# png("world.png", width = 800, height = 400, res = 100)
# Define custom graphic parameters
par(mar=c(0,0,0,0))
# Plot the countries
plot(st_geometry(country), col=cols, border="grey50", lwd=0.5)
# dev.off()
# png("Korea.png", width = 400, height = 400, res = 100)
# Define custom graphic parameters
par(mar=c(0,0,1.2,0))
# Plot countries using the South Korean bounding box
plot(st_geometry(country),
xlim = c(11405000, 11831000),
ylim = c(3736000, 4347000),
col = cols, border="#BB954E", lwd=0.5, bg="#1CB2E7")
# Add the coastline
plot(st_geometry(coastline), col="lightblue", add=T, lwd=1)
# Add label
labelLayer(x = country, txt = 'sovereignt', halo=TRUE)
# Add layout elements
layoutLayer(title = "Zoom on South Korea",
author = "Made with Natural Earth. Free vector and raster map data @ naturalearthdata.com.",
sources = "cartography 2.0.2",
tabtitle = TRUE, frame=FALSE, scale = NULL)
# dev.off()
# Plot the countries
par(mar=c(0,0,0,0))
plot(st_geometry(country), col=cols, border="grey50", lwd=0.5)
# Get a bounding box interactively
loc <- locator(n = 2)
#png("waf.png", width = 800, height = 500, res = 100)
# Define custom graphic parameters
par(mar=c(0,0,1.2,0))
# Plot countries using the interactively defined bounding box
plot(st_geometry(country), col=cols,
border="#BB954E", lwd=0.5, bg="#1CB2E7",
xlim=loc[[1]], ylim=loc[[2]])
# Add the coastline
plot(st_geometry(coastline), col="lightblue", add=T, lwd=1)
# Add labels
labelLayer(x = country, txt = 'name', halo=TRUE)
# Add layout elements
layoutLayer(title = "Zoom on Western Africa",
author = "Made with Natural Earth. Free vector and raster map data @ naturalearthdata.com.",
sources = "cartography 2.0.2",
tabtitle = TRUE, frame=FALSE, scale = NULL)
#dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment