Map of Middle Earth in R
[This article was first published on R-Chart, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Map of Middle Earth
The map above was created using ggplot2, ggmaps, and maptools in R. These are just a few of the great R packages available for cartography and geographic applications. Many readers of this blog will be familiar with these packages and more recent R package additions like leaflet. But great packages are only part of the problem… geographic data about Middle earth is generally found on the inside cover of Tolkien books or perhaps in the Atlas of Middle Earth. And this “data” is just pictures.Fortunately shapefiles are available in a Github Repo that represent many of the significant features of middle earth: https://github.com/jvangeld/ME-GIS. Thanks to the creators of these files (the Middle Earth DEM Project) for your work and generously making these files available online.
If you want to follow along with the R code that follows, clone this repo or download the files to your local machine and extract ’em.
Creating the map above is straightforward… load the required R packages:
library(ggplot2)
library(ggmap)
library(maptools)
The ggplot2 package is used to plot the map. The ggmap package includes the fortify() function which translates the shapefile data into a data frame and the maptools package is used to read the shape files.
Next, navigate to the directory containing the shapefiles and read in the data
setwd(‘/path/to/downloaded/ME-GIS/’)
Read in the data files using maptools.
coastline <- b="" oastline2.shp="" readshapespatial="">->
forests <- b="" orests.shp="" readshapespatial="">->
lakes <- akes2.shp="" b="" readshapespatial="">->
rivers <- b="" ivers19.shp="" readshapespatial="">->
contours <- b="" ontours_18.shp="" readshapespatial="">->
Render the map. Each of the variables containing data is associated with a geom_polygon or geom_path function call.
ggplot() +
geom_polygon(data = fortify(contours),
aes(x = long, y = lat, group = group),
color = ‘#f0f0f0′, fill=’#f0f0f0’, size = .2) +
geom_path(data = fortify(coastline),
aes(x = long, y = lat, group = group),
color = ‘black’, size = .2) +
geom_polygon(data = fortify(forests),
aes(x = long, y = lat, group = group),
color = ‘#31a354′, fill=’#31a354’, size = .2) +
geom_polygon(data = fortify(lakes),
aes(x = long, y = lat, group = group),
color = ‘#a6bddb’, fill=’#a6bddb’, size = .2) +
geom_path(data = fortify(rivers),
aes(x = long, y = lat, group = group),
color = ‘#a6bddb’, size = .2) +
ggtitle(‘Middle Earth’) +
ylab(”) +
xlab(‘Shapefiles: https://github.com/jvangeld/ME-GIS’)
So if you looking for a brief escape into a fantasy realm or hoping to inspire young imaginative cartographers, check out the data at github, load the shapefiles into R using maptools or the rgdal package and get plotting!
To leave a comment for the author, please follow the link and comment on their blog: R-Chart.
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.