Making the Earth move!

R is great, I find it to be a highly versatile language which can be used for more or less anything. In fact, did you know R can make the earth move?

Well it can, and pretty easily too! You only need 3 packages to move the earth which are the ggplot2, maps and animation packages. There are many good resources already out there for instructions on how to download and use each of these libraries, so I’ll skip straight to the good bit.

As always, start by loading in your packages:

library(ggplot2)
library(animation)
library(maps)

Next, we use the borders function from the ggplot2 package to create a layer of international boundaries, which we can then plot. The key part for making a 3d looking globe is the “ortho” part in the call to coord_map().

worldMap <- borders("world", colour = "forestgreen", fill = "green3")

ggplot() + worldMap + coord_map("ortho", orientation= c(40, 0, 0)) + theme(panel.background = element_rect(colour = "black", fill = "black"), panel.grid.major = element_blank())

plot of chunk unnamed-chunk-2

Ok, pretty cool right? So now, we want to be able to create this map, but view it from a different angle each time to create the illusion of rotation. To do this, I’ve created a function to make the same map as above (with some extra stuff to get rid of axes etc.). This function accepts one argument, i, which is the longitude the map centers on.

globMap <- function(i){
map <- ggplot() + worldMap + coord_map("ortho", orientation= c(40, i, 0)) + 
    theme(panel.background = element_rect(colour = "black", fill = "black"), 
    panel.grid.major = element_blank(), axis.line = element_blank(), axis.text.x = element_blank(),
    axis.text.y = element_blank(), axis.ticks = element_blank(), axis.title.x = element_blank(), axis.title.y = element_blank())
    print(map)
    }

Finally, we need to iterate our map function through a sequence of viewing angles to create lots of independant plots, each time from a different angle. To do this I use lapply to change the viewing angle from -180 to 180 degrees in increments of 10 degrees. Wrapping all of this into a function makes our last line to create the GIF image much cleaner.

globeAnimate <- function(){
    lapply(seq(-180, 180, 10), function(x){globMap(x)})
    }

The last line just stitches each plot together to create a GIF movie. You can customize the speed and interval to your liking.

saveGIF(globeAnimate(), interval = 0.15, movie.name= "spinningEarth.gif")
## [1] TRUE

 

spinningEarth

There you have it, R can make the earth move!

Advertisements

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s