Spatial plotting in R Contents Introduction................................................. 1 Plotting various spatial objects...................................... 1 2 2 Using plot()............................................. 2 Using spplot()............................................ 9 2 Introduction One of the biggest advantages of working with spatial data is you can easily visualualize the spatial variation of the data. You do it by making maps. Maps are everywhere! In this session, we will focus on making maps using both raster and spatail class data. Like basic plots, for making maps there are number packages and functions available. However, with the following two functions from sp packages a large number of plotting can be done. 1. plot based on base R plotting system 2. spplot based on grid system plot allows incremental addition of graphical elements in a single plotting device; whereas spplot does not allow such addition (similar to lattice or ggplot2). spplot makes maps with shared axes which makes visualization and comparison of multiple maps much easier. Plotting various spatial objects We will plot various raster (elevation) and vector data (administrative boundaries, rivers, lakes and protected areas) of Tanzania. First we will use the plot function. Make sure your working directory is data directory for this specific excercise. Important Some of the vector data is in *.shp file format while others is in *.rds R database format. You can save anykind of data in *.rds format. Use readrds and saverds to read and save data in *.rds format respectively. 1
Using plot() library(raster) #setwd("...") v0 <- readrds('tza_adm0.rds') plot(v0, border = "red") title(main = "Tanzania national boundary", sub = "source GADM") Tanzania national boundary source GADM Ex 1: Fill the polygon using a different color and change increase the thickness of the border 2
Color individual regions We will use the region boundaries from GADM database (level = 1) v1 <- readrds('tza_adm1.rds') # Each region should have different color; we will use the color ramp n <- length(v1$name_1) plot(v1, col=rainbow(n), main = 'Administrative boundary: level 1') # Now add name of the indiviudal regions to the map text(v1, v1$name_1, cex=0.75) Administrative boundary: level 1 Kagera Mara Mwanza Simiyu Geita Shinyanga Arusha Kilimanjaro Kigoma Katavi Rukwa Tabora Mbeya Singida Dodoma Iringa Manyara Morogoro Tanga Pemba North Pemba South Zanzibar North Zanzibar Zanzibar South West and Central Dar es Salaam Pwani Njombe Lindi Ruvuma Mtwara Ex 2: Read and plot level 2 administrative boundaries of Tanzania. Give different colors to the regions. Add SpatialLines object to your plot We will plot the river. 3
rv <- shapefile('tanzania_rivers.shp') plot(v0, border = "red", lwd = 2, axes = TRUE) plot(rv, col='blue', add = TRUE) title(main = "Tanzania rivers") # Add some more details legend('topright', legend = 'River', lty = 1, lwd = 2, col = 'blue', bty = "n") Tanzania rivers 12 S 10 S 8 S 6 S 4 S 2 S River 30 E 32 E 34 E 36 E 38 E 40 E Ex 3. Change the country boundary color, river color and place the legend in the bottom left of the plot. Add additional SpatialPolygons Now we will plot the lake and protected areas of Tanzania. First read the required boundaries. lake <- shapefile('tza_glwd.shp') protarea <- shapefile('tza_wdpa.shp') plot(v0, lwd = 2, axes = TRUE) 4
plot(lake, col='lightblue', border = 'transparent', add = TRUE) plot(protarea, col='lightgreen', border = 'transparent', add = TRUE) title(main = "Tanzania lakes and protected area") Tanzania lakes and protected area 12 S 10 S 8 S 6 S 4 S 2 S 30 E 32 E 34 E 36 E 38 E 40 E Note the border= transparent option suppresses the plotting of polygon borders. Ex 4. Check what happens when you don t use the border argument in while plotting lake and protected areas. Plot Raster We will plot the elevation of Tanzania. alt <- raster('tza_alt.tif') plot(alt, col = terrain.colors(20), axes=false) title(main = "Elevation (in m)") 5
Elevation (in m) 5000 4000 3000 2000 1000 0 To improve the visualization, we can limit the higher and lower elevataion zones. alt[alt < 0] = 0 alt[alt > 3000] = 3000 plot(alt, col = terrain.colors(20), axes=false) title(main = "Elevation (in m)") 6
Elevation (in m) 3000 2500 2000 1500 1000 500 0 Plotting two raster layers We will show elevation and avergae annual temperature in the same plot. temp <- raster('tza_bio1.tif') temp[temp < 0] = 0 par(mfrow=c(1,2)) plot(alt, col = terrain.colors(20), axes=false) title(main = "Elevation (in m)") plot(temp, col = rev(heat.colors(50)), axes=false) title(main ="Annual Mean Temp ( C)") 7
Elevation (in m) Annual Mean Temp ( C) 3000 2500 2000 1500 1000 500 0 25 20 15 10 5 0 Adding Spatial *object to raster We will add the lake, rivers and administrative boundaries to the elevation raster plot plot(alt, col = terrain.colors(20), legend = FALSE) plot(lake, col='skyblue1', border = 'transparent', add = TRUE) plot(rv, col='blue1', add = TRUE) plot(v0, lwd = 2, axes = TRUE, add = TRUE) title(main = "Lakes and rivers of Tanzania") 8
12 10 8 6 4 2 Lakes and rivers of Tanzania 30 32 34 36 38 40 Using spplot() We will use spplot for different plotting applications. # Use soil properties information; Soil organic carbon content and soil ph orc <- raster('tza_orc.tif') ph <- raster('tza_ph.tif') soil <- stack(orc, ph) orc[orc>80]<- 80 spplot(orc, main = list(label="soil organic carbon content", 9 cex = 1))
Soil organic carbon content 80 70 60 50 40 30 20 10 0 # change the ph values between 0 to 14 ph <- ph/10 spplot(ph, main = list(label="soil ph", cex = 1)) 10
Soil ph 8.0 7.5 7.0 6.5 6.0 5.5 5.0 4.5 4.0 Now to change the color ramp and change legend position: brks <- seq(0,60,0.5) spplot(orc, at = round(brks, digits=2), col.regions = rev(terrain.colors(length(brks))), colorkey = list(space = "bottom"), main = list(label="soil organic carbon content", cex = 1)) 11
Soil organic carbon content 0 10 20 30 40 50 60 Ex 5. Make similar changes to ph plot. Hint: the brks will take place at differnt sequence based on ph values Adding Spatial *object to raster We will add the lake and administrative boundary to the elevation raster plot pols <- list("sp.lines", as(v0, 'SpatialLines')) brks <- seq(0,60,0.5) spplot(orc, sp.layout=pols, at = round(brks, digits=2), col.regions = rev(terrain.colors(length(brks))), colorkey = list(space = "bottom"), main = list(label="soil organic carbon content", cex = 1)) 12
Soil organic carbon content 0 10 20 30 40 50 60 See the difference with plot function. There is no use of add=true argument. You can also add multiple SpatialObjects to the plot. pols1 <- list("sp.lines", as(v0, 'SpatialLines'), col = gray(0.4), lwd = 0.5) pols2 <- list("sp.polygons", as(lake, 'SpatialPolygons'), fill = 'skyblue1',col="transparent", first = F brks <- seq(0,60,0.5) spplot(orc, sp.layout=list(pols1, pols2), at = round(brks, digits=2), col.regions = rev(terrain.colors(length(brks))), colorkey = list(space = "bottom"), main = list(label="soil organic carbon content", cex = 1)) 13
Soil organic carbon content 0 10 20 30 40 50 60 Ex 6. Add the protected area boundaries to the plot. Donot fill the protected area polygon with any color. Plot multiband raster object You can also use ssplot to plot multiband raster brks <- seq(0,70,0.5) ph <- ph*10 soil <- stack(orc,ph) spplot(soil, layout = c(2,1), at = round(brks, digits=2), col.regions = rev(terrain.colors(length(brks))), colorkey = list(space = "bottom"), main = list(label="soil properties", cex = 1)) 14
Soil properties TZA_ORC TZA_PH 0 10 20 30 40 50 60 70 Ex 7.* Save your favorite plot using the following: png(filename = "your-favorite-plot", width = 250, height = 200, units = "mm", res = 300) plot... spplot... dev.off() 15