The globejs function plots surface maps and data points on a sphere that you can spin around and zoom. The function uses the http://threejs.org 3-d visualization library and is part of the threejs package for R, https://github.com/bwlewis/rthreejs. You can install the threejs package using the devtools package:

devtools::install_github("bwlewis/rthreejs")

This document illustrates making globes from images or from maps generated in R. The examples display in RStudio just like regular R plots and can be used in shiny applications or stand-alone web documents created with rmarkdown like this one. When viewing from a web browser we find that recent versions of Google’s chrome and Internet Explorer work very well.

Example 1: Using images as maps on the globe

Any image may be used as a surface map. For example, the globe on the right produced by the following R code uses a beautiful high-resolution NASA “blue marble” MODIS image downloaded from the web.

library("threejs")
earth <- "http://eoimages.gsfc.nasa.gov/images/imagerecords/73000/73909/world.topo.bathy.200412.3x5400x2700.jpg"
globejs(img=earth, bg="white")

You can plot points and bars over the globe by supplying data to the lat and long arguments, using the value and color arguments to control the height and color of each bar. Latitude and longitude values will only align properly with plate carrée equirectangular projection maps, also known as lat/long maps. Fortunately, lat/long map images are commonly available, and they’re also easy to create in R.

Generating maps in R

An impressive variety of sophisticated cartographic packages are available for R, and several of them can easily generate lat/long map images. See the spatial task view for a comprehensive list of available packages.

The maptools package by Roger Bivand and many contributors is a high-level plotting interface to the sp package that greatly simplifies plotting regional and global maps. Our examples illustrate making maps for display on the globe using the maptools package, but with minor modification the examples will work with other R packages like the excellent new rworldmap package.

Example 2: Adding bars to make a spiky globe

Our motivation for this package was the well-known spiky globe visualization http://data-arts.appspot.com/globe/. We can use the globejs function to plot bars over the globe on arbitrary points with just a few lines of code.

library("threejs")
library("maptools")
library("maps")
data(world.cities, package="maps")
cities <- world.cities[order(world.cities$pop,decreasing=TRUE)[1:1000],]
value  <- 100 * cities$pop / max(cities$pop)

globejs(bg="black", lat=cities$lat,     long=cities$long, value=value, 
        rotationlat=-0.34,     rotationlong=-0.38, fov=30)