Follow EcoPress on Twitter

“Like” NREL EcoPress

This is how I did it… Mapping in R with ‘ggplot2’

by Andrew Tredennick

Recently I moved from ArcMap to R do a lot of my spatial analysis and map making. In particular, I’ve started to use the ‘ggplot2’ to create what I think are exceptionally good-looking maps (no offense to ArcMap, but something about ‘ggplot2’ maps are just so crisp). A couple of people have asked for some code to help them on their way with map making in R, so here is a simple snippet of code I used to make a rainfall map of sub-Saharan Africa with a fake transect.

A couple notes.

  1. You will need the ‘raster’ and ‘ggplot2’ packages installed. In R this is easily achieved from command line via, install.packages(“ggplot2”) and install.packages(“raster”). Some R interfaces (like RStudio) also have easy-to-use GUIs to help install new packages
  2. In this code I am creating the map from an ASCII file as exported from ArcMap (it was a raster I exported as ASCII).

The code.

rm(list=ls()) #clear workspace

#load libraries
library(raster)
library(ggplot2)

#open ASCII file using ‘raster’ command, which converts the ASCII to a raster object
map <- raster(“/your/path/to/file.asc”)

#convert the raster to points for plotting
map.p <- rasterToPoints(map)

#Make the points a dataframe for ggplot
df <- data.frame(map.p)
#Make appropriate column headings
colnames(df) <- c(“Longitude”, “Latitude”, “MAP”)

#Call in point data, in this case a fake transect (csv file with lat and lon coordinates)
sites <- data.frame(read.csv(“/your/path/to/pointfile.csv”))

#Now make the map
ggplot(data=df, aes(y=Latitude, x=Longitude)) +
geom_raster(aes(fill=MAP)) +
geom_point(data=sites, aes(x=x, y=y), color=”white”, size=3, shape=4) +
theme_bw() +
coord_equal() +
scale_fill_gradient(“MAP (mm/yr)”, limits=c(0,2500)) +
theme(axis.title.x = element_text(size=16),
axis.title.y = element_text(size=16, angle=90),
axis.text.x = element_text(size=14),
axis.text.y = element_text(size=14),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = “right”,
legend.key = element_blank()
)

The product.

I’ve only allowed the colors to go up to 2,500 mm annual rainfall because I wanted to show how most of Africa has rainfall levels below 2,500 mm/yr. You can change this by messing with colors and the ‘scale_fill_gradient()’ command. ggplot2 has great online resources here, http://docs.ggplot2.org/current/#.

Rplot01

If you have any questions (e.g., about the specific commands within the ggplot call), post them in the comments and I’ll address them.

From the comments: here’s a comparison using the base R plotting commands. I didn’t work to match the colors because I was using default ggplot2 colors and wanted to compare with default base colors (one of the many great things about ggplot2 is pleasing default color options). Also notice the difference in tick positions and spacing. Of course, I could make this look a lot better, but that would take more work than it takes using ggplot2. Just my opinion though!

Rplot03

Do you have any great science tricks you want to share? Email us at nrel.ecopress@gmail.com or tell us on Facebook, http://www.facebook.com/nrelscience.