Animated great circles on rotating 3D Earth: Example R code

Making still frames for great circle animations in R

Matthew Leonawicz

Here I share R code I used to produce animated great circle arcs on top of a rotating 3D Earth. The code is not entirely reproducible but you should be able to use what is shared here to create your own video frames given your unique data and computing environment and resources.

The WordPress blog is not the most elegant for displaying lots of code so go to the original full post.

When I make great circle animations, at the core of the process is always an R function that transforms a series of coordinates describing points along a great circle arc into multiple series of great circle arc segments. The goal is simple: plot a series of line segments, saving each plot as a subsequent still frame, rather than plotting the original entire arc as a single plot. The input is generally a data table (much faster to work with than a data frame if you have a lot of data) with longitude and latitude columns where the coordinates in each row describe a subsequent point along one of my paths. I also use a third column to provide a unique group ID for each path to keep them distinct.


R animation: global SNAP Shiny app users

For the blog readers, just a quick heads up that I have posted a new R data animation to YouTube. A complete post will follow, but for now here is the video. It displays the social network of SNAP Shiny app users over about the past year and a half using great circle trajectories on a rotating 3D Earth. It’s best in 1080p, but still somewhat degraded for streaming. I’ll post the raw source video later as well, which is crystal clear.

I used geolocation data from Google Analytics. I routed all the traffic randomly through either Fairbanks, AK or Denver, CO to complete the network since those two places best describe where my apps come from. It’s great to see how many people connect to the apps and from where; too many people to plot as a single static graphic without some of the essence of the data being lost.

As usual, the still frames of the rotating globe, its surface texture, country boundaries and great circle paths are all made in R. It’s basically a series of plots made with ggplot2.

R Shiny leaflet: using observers

Using leaflet in R is fairly simple and there are plenty of online resources and examples available already. See the RStudio tutorial to get started if you haven’t already. This post presents a series of examples which build upon each other.


The code displays both point data and raster data on leaflet maps with Shiny app integration. The focus here is to show the utility of event observation on the server side for integrating the behavior of browser elements, namely, the leaflet map.

In the course of these examples, I’ll also use a modal from the shinyBS package and the maps will be full size in the browser window with UI widgets laid on top using absolute panels. The full post contains interactive embedded Shiny widgets. See the full post here. It includes complete source code for these and other related apps using leaflet.


Upload shapefile to R Shiny app to extract leaflet map data

In this post I share an R Shiny app which uses the leaftlet package for interactive maps. This app differs from prior apps I’ve made featuring leaflet maps. First, it displays rasterized map data rather than just point layers. Also, longitude and latitude sliders in the browser allow for cropping the map. Additionally, the user can upload a shapefile to crop and mask the rasterized data overlays in the leaflet window to the specific spatial data they wish to work with, and then extract and download that data.


R + htmlwidgets + DT + sparkline

Here are some examples with code where I use the R packages DT and sparkline to produce interactive data tables and inline graphs as well as inline graphs within data table cells. Both packages utilize the htmlwidgets package to interface with javascript libraries. With htmlwidgets you can create custom widgets to use specific javascript libraries to display your data from R on the web. I used the above packages along with rmarkdown to make a markdown document containing the example widgets and code, but you can also use widgets within Shiny apps.


R animation: great circles 3, final draft

Here is the final draft of my great circle arcs R animation. I made this back in January shortly after my first two drafts, but am only now getting around to sharing it, which is a typical representation of how seldom I can make time for blogging. But given the recent spike in interest (thanks to Urban Demographics for sharing my work) in the first, and roughest, draft, I am motivated to finally share something better.

As with DRAFT 1 and DRAFT 2, the YouTube video upload is of significantly reduced quality compared to the original render. As I’ve said in the previous posts, it’s really not even worth watching on YouTube.

A much higher quality source video is available here. It can be downloaded (~365 MB) and viewed locally using a standard video player such as VLC. Here is a screenshot taken from source as an example:


R plot: Comparison of Fairbanks, Alaska and Beijing, China air quality

Here’s an interesting R plot comparing a specific air pollution metric between Fairbanks, Alaska and Beijing, China. Right off the bat, Beijing obviously has far worse air quality, and more significantly, it is a chronic, daily problem. But it is used for comparison because we already know this is the case.

In Fairbanks, while air quality is known to decrease in very cold weather and the seasonal cycle of winter spikes in the particulate are clear in the plot, this is nothing compared to Beijing’s daily levels year round. On the other hand, lots of summertime smoke from boreal forest fire in interior Alaska can make Fairbanks air quality just as bad as Beijing’s if not worse. This can be seen in 2004, which was a year of record fires in Alaska. 2015 has also been a big fire season, apparent in the plot as well.