3D rotating Earth in R

Recently I posted some 3D Earth graphics here and here made with R. Now I have a basic animation showing different applied textures on a rotating globe.

You will see some moments where points and lines are overlaid on the Earth in addition to the landscape and choropleth textures. Note that I did not plot these as supplemental individual points and lines in each frame of the animation. I will have that to share later, but it is more challenging to program efficiently.

Instead, these too are merely textures wrapped on the sphere. If you look closely, there are moments where you may be able to see that the 2D images of the points and lines, respectively, cannot be wrapped to perfectly match up with the landscape textures. Line matching is particularly problematic at the dateline in this example.

There is a cost to quality by using textures in rgl but the cost is minimal vs. the gains in efficiency when applied to the right kind of texture, e.g., a spatially explicit geographic image. For some plotting elements, however, such as great circle arcs, you have to chew up more resources to achieve good quality by application of direct and explicit plotting methods. This is easy in lightweight cases, but can be difficult if you have an enormous number of things to plot.

This entry was posted by Matt Leonawicz.

2 thoughts on “3D rotating Earth in R

    • Thank you. I don’t have anything that would enable others to reproduce exactly that. However, the bulk of it can be broken down into two steps. The first is a single line. Use rgl.spheres() (spheres3d() is a wrapper to this; that might be fine too), with the texture=”your.png” argument, where your PNG is any full extent WGS84 map image. There’s your globe!

      If you look at the function code and documentation you’ll see that texture is passed to rgl.material(). There are vignettes and tutorials online, in addition to the rgl manual. I recommend specifically becoming familiar with rgl.material(), especially the arguments which relate to the visibility of overlaid textures depending on their radius from the center of the sphere, their plot order, and how rgl.material() is told to handle the alpha channels in your images. You’ll want your textures to be properly prepared. When I show a bathymetry layer only over water but another texture over land, it is because I first saved typical 2D plots from R where I had masked the data for this very purpose, e.g., setting elevations > 0 to NA in the former, and saving both images with a transparent background.

      Once you have a globe, or any rgl plot, you can rotate it about different axes. Instead of dragging it by hand using the mouse, alter the perspective using rgl.viewpoint(). Change its theta argument by a little bit inside a for loop as you iterate and the globe will rotate. Also inside this loop, you can save stills by using rgl.snapshot(). You can also look up some stock functions, like play3d(), movie3d(), and spin3d(). These are made for a purpose, but therefore more restrictive. I find it more appealing to put arbitrary rgl commands inside my own for loop, or I may just not be experienced enough with controlling these other functions yet.

      Of course, the fancy text that fades in and out in the video is dropped on top of the frames after I load the frames into a video editor to render the movie. The fading on the globe of one texture into another can be done in R with alpha levels as part of your frame generation or afterward. If you’re going to dump frames into a video editor anyway, the latter may be easier. It doesn’t make sense to do everything in a single program, even if that program is R. Also, you probably wouldn’t want to “hard code” such nuanced changes into your image frames anyway, so that they are full and bright and flexible for various uses after production in R.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: