Maps in R

Rosalie Bruel
June 1st, 2019

Content - 1. Working with .tif files in R - 1.1. Visualization - 1.2. Histogram plot - 2. Different types of visualization. - 3. Other interesting resources


I used QGIS to compute the bathymetry from xyz data I had available for Lake Champlain (tutorial here), but I carry all my analyses in R. I’m gathering here a few lines of code I found to be useful, as I may need them later.

1. Working with .tif files in R

1.1. Visualization

Leah A. Wasser came up with an excellent tutorial on using the raster and rasterVis libraries.

In QGIS (tutorial here), we created a .tif layer, that we can read into r using the following code.

library(raster)
# read data
bathy <- raster("GIS/bathy.tif")
plot(bathy,
     main = "Bathymetric map of Lake Champlain",
     col = grey(1:100/100),
     axes = FALSE,
     box = FALSE)

The rasterVis package provides several fancy visualization options. I adapted the code I found here.

library(rasterVis)
# Define a colour ramp palette
colr <- colorRampPalette(brewer.pal(11, 'RdYlBu'))

p <- levelplot(bathy, 
          margin=TRUE,       # turn to F to suppress marginal graphics
          colorkey=list(
            space='bottom',  # where to plot legend
            labels=list(at=seq(-120,0,20))      # legend ticks and labels
            ),    
          par.settings=list(
            axis.line=list(col='black') # turn to transparent to suppress axes and legend outline
          ),
          scales=list(draw=TRUE),    # turn to F to suppress axis labels
          col.regions=colr,          # colour ramp
          at=seq(-130, 0, len=130))  # colour ramp breaks

# plot the output
p

‘levelplot’ creates an object of class trellis (with lattice methods and grid objects). Trellis graphs display a variable or the relationship between variables, conditioned on one or more other variables.
Lattice plots include histogram() and dotplot() for example. An interesting feature of the lattice environment it allows to set a particular theme to plots, for instance, if you want to permanently change the background of your plots. The process is very well summarized in this blog post.

library(lattice)
# Show the current settings
# show.settings()

# Current theme
my.theme = trellis.par.get()
# Change panel background color
my.theme$panel.background$col   = grey(.8) # set background to grey
my.theme$panel.background$alpha = .3       # set transparency

# Change axis line width and axis text size
my.theme$axis.line$lwd    = 1.5 # default was 1
my.theme$axis.text$font   = 2

# Update setting
trellis.par.set(my.theme)

For those more familiar with ggplot2, it’s the equivalent of theme_set().

1.2. Histogram plot

The rasterVis library has tons of other cool visuals. For example, I like the one showing an histogram of the object’s depths. It provides a visual way to compare lakes shape (shallow vs. hollow, Meybeck, 1995). To illustrate this, here are the histogram I get from the .tif files of Lake Champlain, Lake Ontario, and Lake Geneva.

histogram(bathy_champlain, main='a. Lake Champlain depth histogram', xlab='depth (m)')
histogram(bathy_leman, main='b. Lake Geneva depth histogram', xlab='depth (m)')
histogram(bathy_ontario, main='c. Lake Ontario depth histogram', xlab='depth (m)')

The three lakes have really contrasted depth profiles. Lake Chmplain has many shallow basins, Lake Geneva is very steep and has a large deep pelagic zone (see the high count for depths around 280 m), while Lake Ontario is somewhere between the two.

2. Different types of visualization.

Leaving here some visuals I like and may re-use.

Package urbanmap (blog post here).

library(devtools)
devtools::install_github("UrbanInstitute/urbnmapr")
library(urbnmapr)

3. Other interesting resources

Well made and concise tutorial on getting started with the raster and rgdal packages.

is loading comments…