Map of Middle Earth in R

October 8, 2016

(This article was first published on R-Chart, and kindly contributed to R-bloggers)

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:  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:


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


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(”) + 

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. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)