<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/building-an-interactive-globe-visualization-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.displayr.com/interactive-globe-r/?utm_medium=Feed&utm_source=Syndication"> R – Displayr</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In <a rel="nofollow" href="https://www.displayr.com/chances-hit-by-meteorite/" target="_blank">this earlier post</a> we analysed the location of meteorite impacts from this <a rel="nofollow" href="https://data.nasa.gov/Space-Science/Meteorite-Landings/gh4g-9sfh" target="_blank">dataset</a>, including plotting their fall locations on a globe. Forming part of the analysis was this interactive globe visualization below, which plots the location and age of meteorites.</p>
<p><iframe width="450" src="https://embedded.azureedge.net/81491/286419/e9437584-33af-4557-8aed-72c3848f19af.html?v=32192-3c733eabf4-1363549943"></iframe></p>
<p>In this post I am going to show how to create this globe with code. I then go on to describe other options and variations on the same theme.</p>
<h2>Preparing and plotting the data</h2>
<p>The code below does everything we need. Stepping through each section:</p>
<ul>
<li>First, import the data then convert from the year the meteorite fell to an age in years.</li>
<li>Tidy the data frame to collect just the variables that we need.</li>
<li>Convert <em>age </em>into a number from 1 to 10 (the lowest 10% of ages map to 1, the next 10% to 2 .. etc), then map those numbers to shades of red.</li>
<li>Finally, plot the data on the globe where <em>val</em> (the mass) determines the length of each line and fixed <em>pointsize</em> determines the line thickness.</li>
</ul>
<pre>
library(threejs)
library(flipChartBasics)

# Read the data and calculate age in years
x = read.csv(&quot;https://data.nasa.gov/api/views/gh4g-9sfh/rows.csv&quot;)
current = as.numeric(format(Sys.Date(), &quot;%Y&quot;))
x$age = current - as.numeric(substr(x$year, 7, 10))

# Filter the required information
x = x[ , c(&quot;reclong&quot;, &quot;reclat&quot;, &quot;mass..g.&quot;, &quot;age&quot;)]
colnames(x) = c(&quot;long&quot;,&quot;lat&quot;,&quot;value&quot;, &quot;age&quot;)

# Set colors on a scale of 1 to 10 by percentile
colors = as.numeric(cut(x$age,
breaks = quantile(x$age, probs = seq(0, 1, 0.1), 
                  include.lowest = TRUE, na.rm = TRUE)))
palette = ChartColors(10, &quot;Reds&quot;, reverse = TRUE)
colors = palette[colors]

# Plot the data on the globe
globejs(lat = x$lat,
        long = x$long,
        val = 2 * log(x$value),
        color = colors,
        pointsize = 0.5,
        atmosphere = TRUE)
</pre>
<p>The output is slightly different from the original in the <a rel="nofollow" href="https://www.displayr.com/chances-hit-by-meteorite/" target="_blank">meteorites post</a> because we have not excluded any data. You can spin the globe around with your mouse and zoom in or out.</p>
<p><iframe width="450" src="https://embedded.azureedge.net/75668/296842/e9437584-33af-4557-8aed-72c3848f19af.html?v=32474-1ee9900510-972089864"></iframe>
</p>
<h2>Capital cities</h2>
<p>As a second example, we’ll plot capital city data <a rel="nofollow" href="https://esa.un.org/unpd/wup/cd-rom/WUP2014_XLS_CD_FILES/WUP2014-F13-Capital_Cities.xls" target="_blank">available here</a>. The data is imported with the <span>DownloadXLSX</span> function from the <span>flipAPI</span> package.</p>
<p>The <em>Population (thousands)</em> column is imported as a factor. We amend it to be a number. The code to make the globe is broadly similar to that above.</p>
<pre>
library(threejs)
library(flipChartBasics)
library(flipAPI)

# Make a data.frame of the required information
url &lt;- &quot;https://esa.un.org/unpd/wup/cd-rom/WUP2014_XLS_CD_FILES/WUP2014-F13-Capital_Cities.xls&quot;
x = DownloadXLSX(url, skip = 15)
x = x[, c(&quot;Longitude&quot;, &quot;Latitude&quot;, &quot;Population (thousands)&quot;, &quot;Capital City&quot;)]
names(x) = c(&quot;long&quot;,&quot;lat&quot;, &quot;population&quot;, &quot;city&quot;)

# Convert population to numeric
x$population = as.character(x$population)
x$population[x$population == &quot;\u2026&quot;] = 0 # remove ellipsis
x$population = as.numeric(x$population)

# Set colors according to first letter of the city name
first.letters = sapply(substring(x$city, 1, 1), 
                       utf8ToInt) - utf8ToInt(&quot;A&quot;) + 1
palette = ChartColors(26, &quot;Blues&quot;)
colors = palette[first.letters]

# Plot the data on the globe
earth = &quot;http://eoimages.gsfc.nasa.gov/images/imagerecords/73000/73909/world.topo.bathy.200412.3x5400x2700.jpg&quot;
globejs(img = earth,
        lat = x$lat,
        long = x$long,
        val = 10 * log(x$population), 
        color = colors,
        pointsize = 5,
        atmosphere = FALSE,
        bg = &quot;white&quot;)
</pre>
<p><iframe width="450" src="https://embedded.azureedge.net/75668/296842/dab528f4-f3e0-46c4-a28d-57e99dffe718.html?v=32474-1ee99005101791339684"><span data-mce-type="bookmark">﻿</span></iframe>
</p>
<p>A few notable differences are:</p>
<ul>
<li>Addition of a more colorful background world image via the <em>img </em>argument.</li>
<li>Line lengths related to population and colors according to the first letter of the city name (light blue = A, dark = Z).</li>
<li>Removal of <em>atmosphere</em>, increase of <em>pointsize</em> and amending the default background (<em>bg) </em>color.</li>
</ul>
<p>Finally, the globe image can be changed. If you wanted to confuse people by presenting the same information with an arbitrary citrus theme, you could make the following, again using the <em>img</em> argument.</p>
<p><iframe width="450" src="https://embedded.azureedge.net/75668/296842/867e75b3-c145-46ab-b501-7d1dd68c62aa.html?v=32474-1ee9900510-1498556427"></iframe>
</p>
<h2></h2>
<p><strong>The globes are created with the <a rel="nofollow" href="http://bwlewis.github.io/rthreejs/" target="_blank">threejs</a> package. You can see and modify the examples in this post by <a rel="nofollow" href="https://app.displayr.com/Try/Globe%20visualization" target="_blank">following this link</a>.</strong></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="https://www.displayr.com/interactive-globe-r/?utm_medium=Feed&utm_source=Syndication"> R – Displayr</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 rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/FPuDpcPIJmY" height="1"></a></div>