Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
When I was a kid, I was was fascinated by the conundrum of what happens when you drill a hole straight through the centre of the earth. I always believed that I would turn up in Australia. But is this really the case?
The antipodes of any place on earth is the place that is diametrically opposite to it. A pair of antipodes are connected by a straight line running through the centre of the Earth. These points are as far away from each other as is possible on this planet. Two people are antipodes when they live on opposite sides of the globe. Their feet (πούς/pous in Ancient Greek) are directly opposite each other.
How can we use coding in R to solve this conundrum?
Using R to find antipodes
The package contains a data file with major coastlines that can be used to create a flipped map of the world. To turn a spatial location into its antipode you subtract 180 degrees from the longitude and reverse the sign of the latitude, shown below.
library(globe) # Create antipodean map flip <- earth flip$coords[,1] <- flip$coords[,1]-180 flip$coords[,2] <- -flip$coords[,2] par(mar=rep(0,4)) # Remove plotting margins globeearth(eye=c(90,0), col="blue") globepoints(loc=flip$coords, eye=c(90,0), col="red", pch=".")
We can also use the ggmap package to visualise antipodes. This package, developed by David Kahle antipodean R-guru Hadley Wickham, has a neat geocoding function to obtain a spatial location. The antipode function takes the description of a location and a zoom level to plot a dot on the antipode location. The gridExtra package is used to created a faceted map, which is not otherwise possible in ggmap.
library(ggmap) library(gridExtra) antipode <- function(location, zm=6) { # Map location lonlat <- geocode(location) loc1 <- get_map(lonlat, zoom=zm) map1 <- ggmap(loc1) + geom_point(data=lonlat, aes(lon, lat, col="red", size=10)) + theme(legend.position="none") # Define antipode lonlat$lon <- lonlat$lon-180 lonlat$lat <- -lonlat$lat loc2 <- get_map(lonlat, zoom=zm) map2 <- ggmap(loc2) + geom_point(data=lonlat, aes(lon, lat, col="red", size=10)) + theme(legend.position="none") grid.arrange(map1, map2, nrow=1) } antipode("Rector Nelissenstraat 47 Hoensbroek", 4)
This code solves the problem I was thinking about as a child. Running the code shows that the antipodes location of the home I grew up in is not in Australia, but quite a way south of New Zealand. Another childhood fantasy shattered …
The post Finding antipodes using the globe and ggmap packages appeared first on Data Science with R.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.