<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/spinning-globes-with-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://spatial.ly/2017/05/spinning-globes-with-r/"> R – Spatial.ly</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>It has been a long held dream of mine to create a spinning globe using nothing but R (I wish I was joking, but I’m not). Thanks to the brilliant <a target="_blank" rel="nofollow" href="https://github.com/leonawicz/mapmate">mapmate</a> package created by <a target="_blank" rel="nofollow" href="https://leonawicz.github.io/">Matt Leonawicz </a>and shed loads of computing power, today that dream became a reality. The globe below took 19 hours and 30 processors to produce from a relatively low resolution <a target="_blank" rel="nofollow" href="https://earthobservatory.nasa.gov/Features/NightLights/page3.php">NASA black marble</a> data, and so I accept R is not the best software to be using for this – but it’s amazing that you can do this in R at all!</p>
<p><img src="https://i2.wp.com/dataviz.spatial.ly/black_marble_animation.gif?w=450" data-recalc-dims="1"></p>
<p>The code I used to do this is posted below. Under the hood the <a target="_blank" rel="nofollow" href="http://ggplot2.org/">ggplot2</a> package is used for the plotting, so the first few data prep steps are taking the raster and converting it into a format that works with that. For information about installing the required packages and some other examples see the mapmate vignette <a target="_blank" rel="nofollow" href="https://github.com/leonawicz/mapmate/blob/master/vignettes/mapmate.Rmd">here</a> – there’s a lot more to it than the below. The black marble dataset is from <a target="_blank" rel="nofollow" href="https://earthobservatory.nasa.gov/Features/NightLights/page3.php">here</a>.</p>
<p> </p>
<pre><code>#Load in the packages
library(raster)
library(mapmate)
library(dplyr)
library(geosphere)
library(data.table)
library(&quot;parallel&quot;)
library(purrr)
library(RColorBrewer)
library(classInt)

#Load in the raster data
marble&lt;-raster(&quot;BlackMarble_2016_3km_geo.tif&quot;)

#Simplify the raster - to test this out I would set the value at 50+
marble&lt;- aggregate(marble, 10)

#Extact the values and coordinates from the raster grid - this is what is passed onto ggplot2.
marble.pts&lt;-rasterToPoints(marble, spatial=T)
marble.pts@data &lt;- data.frame(marble.pts@data, long=coordinates(marble.pts)[,1],lat=coordinates(marble.pts)[,2]) 
names(marble.pts@data)&lt;- c(&quot;z&quot;,&quot;lon&quot;,&quot;lat&quot;)

#Convert to the tidyverse, required for mappmate.
marble.dat&lt;-as_data_frame(marble.pts@data)

#We aren't plotting the image colours - rather a series of rectangles to be coloured by the pixel value. The range of colours needs to be specified. For this I have extraced the main colours from NASA's image and approximately aligned them to their corresponding values. This is seen the colour palette below that gets fed into the map.
pal&lt;-colorRampPalette(c(&quot;#0b0c1a&quot;,&quot;#1e1c37&quot;,&quot;#202144&quot;,&quot;#2b3355&quot;,&quot;#7f6e61&quot;,&quot;#d0b695&quot;,&quot;#efd7af&quot;, &quot;#fefbe6&quot;), bias=2.75)
n&lt;-30

#some more magic here - see the vignette I link to above.
marble.frame &lt;- map(1:n, ~mutate(marble.dat, frameID = .x))
rng &lt;- range(marble.dat$z, na.rm=TRUE)
file &lt;- &quot;3D_rotating_simp&quot;
id&lt;- &quot;frameID&quot;

#OK - here goes! You need the parallel package up and running - mc.cores specifies how many processors to use. You can see I used 30 but this can obviously be less.

mclapply(marble.frame, save_map,z.name=&quot;z&quot;, id=id, lon=0, lat=0, n.period=30, n.frames=n, col=pal(5000), type=&quot;maptiles&quot;, file=file, z.range=rng,png.args = list(width = 30, 
        height = 30, res = 300, bg = &quot;transparent&quot;, units=&quot;cm&quot;),rotation.axis = 0,mc.cores=30)</code></pre>
<p></p>
<p><br>
</p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://spatial.ly/2017/05/spinning-globes-with-r/"> R – Spatial.ly</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/44IzAYokc6g"></a></div>