Skip to content

Instantly share code, notes, and snippets.

@rCarto
Last active January 24, 2022 11:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rCarto/0c735ccbe09884fb8ee6bac7e457cac8 to your computer and use it in GitHub Desktop.
Save rCarto/0c735ccbe09884fb8ee6bac7e457cac8 to your computer and use it in GitHub Desktop.
library(rnaturalearth)
library(mapsf)
library(fisheye)
ct <- ne_download(scale = 50, returnclass = "sf")
oc <- ne_download(type = "ocean", category = "physical",
scale = 50, returnclass = "sf")
x <- st_transform(ct, "+proj=natearth2")
z <- st_transform(oc, "+proj=natearth2")
mf_export(z, "initial.png", theme = "green")
mf_map(z, col = "white", border ="white", add = T)
mf_map(x, col = "lightblue", border = "white", lwd = .7, add = T)
mf_map(x, "POP_EST", "prop", border = "grey80",
inches = .3, leg_title = "Population", leg_frame = T)
mf_title("Original Map")
dev.off()
xCHN <- fisheye(x = x, centre = x[x$ADM0_A3 %in% "CHN",], k = 6)
zCHN <- fisheye(x = z, centre = x[x$ADM0_A3 %in% "CHN",], k = 6)
mf_export(zCHN, "china.png", theme = "green")
mf_map(zCHN, col = "white", border ="white", add = T)
mf_map(xCHN, col = "lightblue", border = "white", lwd = .7, add = T)
mf_map(xCHN, "POP_EST", "prop", border = "grey80",
inches = .3, leg_title = "Population", leg_frame = T)
mf_title("Transformed map, center = China")
dev.off()
xNGA <- fisheye(x = x, centre = x[x$ADM0_A3 %in% "NGA",], k = 6)
zNGA <- fisheye(x = z, centre = x[x$ADM0_A3 %in% "NGA",], k = 6)
mf_export(zNGA, "nigeria.png", theme = "green")
mf_map(zNGA, col = "white", border ="white", add = T)
mf_map(xNGA, col = "lightblue", border = "white", lwd = .7, add = T)
mf_map(xNGA, "POP_EST", "prop", border = "grey80",
inches = .3, leg_title = "Population", leg_frame = T)
mf_title("Transformed map, center = Nigeria")
dev.off()
xCHE <- fisheye(x = x, centre = x[x$ADM0_A3 %in% "CHE",], k = 6)
zCHE <- fisheye(x = z, centre = x[x$ADM0_A3 %in% "CHE",], k = 6)
mf_export(zCHE, "switzerland.png", theme = "green")
mf_map(zCHE, col = "white", border ="white", add = T)
mf_map(xCHE, col = "lightblue", border = "white", lwd = .7, add = T)
mf_map(xCHE, "POP_EST", "prop", border = "grey80",
inches = .3, leg_title = "Population", leg_frame = T)
mf_title("Transformed map, center = Switzerland")
dev.off()
@rCarto
Copy link
Author

rCarto commented Jan 24, 2022

A possible workflow could be:

  1. download covid data at country level, with ISO3 country codes
  2. import the dataset as a data.frame
  3. merge the country sf object (x here) and the covid data.frame using the relevant common variables (iso3 something...)
  4. create the map (see https://riatelab.github.io/mapsf/ for how to use mapsf)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment