https://github.com/mdsumner/cartogony
globes and maps in R
https://github.com/mdsumner/cartogony
cartography gis maps rstats spatial-data
Last synced: 8 months ago
JSON representation
globes and maps in R
- Host: GitHub
- URL: https://github.com/mdsumner/cartogony
- Owner: mdsumner
- Created: 2016-08-14T13:11:25.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-02-04T13:43:10.000Z (over 8 years ago)
- Last Synced: 2024-12-28T12:17:23.321Z (10 months ago)
- Topics: cartography, gis, maps, rstats, spatial-data
- Language: R
- Size: 22.8 MB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.Rmd
Awesome Lists containing this project
README
---
output: github_document
---## Globey globes in R
These are hard to do for several reasons:
* topology is not present in most map data and tools for them
* 'sp::spTransform' will not return a result with missing coordinates
* 'rgdal::project' only works on raw coordinates
* individual plot frames take time, so need to be cached (or the preparation cached)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.
```{r setup, include=FALSE}
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("", options$label, paste0(opts_knit$get("base.url"), mov.fname))
})opts_chunk$set(cache = TRUE, message = FALSE, warning = FALSE, fig.show = "animate")
``````{r prep}
## 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())
``````{r anim}
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)
}
```
```{r anim2}
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)
}
```