Globey globes in R

These are hard to do for several reasons:

This readme shows a couple of ways to get around all but the topology problems, and may in time become a way to explore solutions for the topology as well. I think the best way will be to use RTriangle, which has the bonus that that's also the way to plot stuff in 3D.

For the animation I used the approach in the 'gganimate' package README. NOTE: as well as ImageMagick for a standalone GIF, you need named chunks in the RMarkdown for this to work.

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "README/README-fig-",
  cache.path = "README/README-cache-"
)

library(animation)
library(knitr)
ani.options(autobrowse = FALSE, interval = 0.2)

opts_knit$set(animation.fun = function(x, options, format = "gif") {
  x = c(knitr:::sans_ext(x), knitr:::file_ext(x))
  fig.num = options$fig.num
  format = sub("^[.]", "", format)
  fig.fname = paste0(sub(paste0(fig.num, "$"), "*", x[1]), 
                     ".", x[2])
  mov.fname = paste0(sub(paste0(fig.num, "$"), "", x[1]), ".", 
                     format)

  # order correctly
  figs <- Sys.glob(fig.fname)
  figs <- figs[order(as.numeric(stringr::str_match(figs, paste0("(\\d+)\\.", x[2]))[, 2]))]

  animation::im.convert(figs, output = mov.fname)

  sprintf("![%s](%s)", options$label, paste0(opts_knit$get("base.url"), mov.fname))
})

opts_chunk$set(cache = TRUE, message = FALSE, warning = FALSE, fig.show = "animate")
## to snapshot the frame loop
library(animation)
## for PROJ4
library(rgdal)  
## for a simple world coastline
library(maptools) 
## for longlat lines 
library(graticule) 
 ## for working with spatial in a  generic way
library(spbabel) 
## for functions to project coordinates in a data frame version of Spatial
library(cartogony)  
library(rworldmap)
data(countriesLow)
wrldtab <- sptable(countriesLow)

grat0 <- sptable(graticule())
rotseq <- head(seq(-180, 180, length = 36), -1L)
lat <- -42
for (i in seq_along(rotseq)) {
grat <- projDFcolumns(grat0 , prjmaker(rotseq[i], lat))
p <- par(mar = rep(0, 4))
plot(grat$X, grat$Y, pch = ".", asp = 1, axes = FALSE, xlab = "", ylab = "")
lapply(split(grat, grat$branch_), function(x) lines(x$X, x$Y))


wrld0 <- projDFcolumns(wrldtab, prjmaker(rotseq[i], lat))
junk <- lapply(split(wrld0, wrld0$branch_), function(x) try(polygon(x$X, x$Y, col = "grey"), silent = TRUE))

par(p)

}
rotseq <- head(seq(-180, 180, length = 36), -1L)
lat <- 42
for (i in seq_along(rotseq)) {
grat <- projDFcolumns(grat0, prjmaker(rotseq[i], lat))
p <- par(mar = rep(0, 4))
plot(grat$X, grat$Y, pch = ".", asp = 1, axes = FALSE, xlab = "", ylab = "")
lapply(split(grat, grat$branch_), function(x) lines(x$X, x$Y))


wrld0 <- projDFcolumns(wrldtab, prjmaker(rotseq[i], lat))
junk <- lapply(split(wrld0, wrld0$branch_), function(x) try(polygon(x$X, x$Y, col = "grey"), silent = TRUE))

par(p)

}


mdsumner/cartogony documentation built on May 22, 2019, 4:43 p.m.