You are on page 1of 3

Lab 7 SUPPLEMENTING QGIS WITH R

Last modified 7 May 2014


NEW SKILLS
Further use of R to carry statistical analysis on geographic data sets

In this lab we will continue to extract data from bands of the aerial images of Field 5. The file
decIR.tif is the IR band of a false color infrared aerial image of bare soil just after a heavy
rainstorm. Wet soil absorbs infrared energy more than dry soil. Therefore in the image the areas
with low values are wetter because of less water infiltration. The amount of water infiltration may
be related to clay content of the soil. The clay content was measured at the soil sample points in
the feature point layer f05points. We will compare the water infiltration infrared data, prepared as
integer points, with the clay content, prepared as integer points using a scatterplot in R.

1. We will start by using QGIS to locate the sample points. Start QGIS and the raster layer
decIR.tif and the vector layer f5samples.shp from the folder C:\QGISLab\Wheat. The file
decIR.tif is a geotiff file, so the f5samples shapefile should overlay the image. Verify that this is
the case.

We will carry out the calculations for this exercise in R. Many of the functions needed to carry
out this exercise involve detailed programming of R. If you wish to delve more deeply into these
functions, see the documentation on the R website, especially An Introduction to R by
Venables and Smith.

2. Start the R program. To carry out the calculations we will need functions from the package
maptools, which you downloaded in lab 6, and also from the package rgdal. Following the same
steps as in Lab 6, install the package rgdal and then give the R commands

library(maptools)
library(rgdal)

First we will load and display the image file decir.tif. Type the commands (modified
appropriately for your file structure). Remember that R is case sensitive, although the file names,
which are simply passed to Windows, are not.

f5.grid <- readGDAL("c:\\ qgislab\\wheat\\gis\\decir.tif")


image(f5.grid)

Again, note that in R you need double slashes to indicate the directories. You should see an image
of the wheat field, although it may be in a strange color. NOTE: From now on it may not be
evident what the R functions are doing. Dont forget that you can read about any R function by
typing the name of that function with a ? in front of it.

3. The next step is to read in the point data. The function we use for this shown here

f5.pts <- readShapePoints("c:\\ qgislab\\wheat\\field5samples.shp")

To see what the structure of f5.pts is, type:

str(f5.pts)

The clay data is in f5.pts$CLAY (remember that R is case sensitive!).


4. Next we will overlay the point data onto the grid data.

x <- overlay(f5.grid,f5.pts)

To find out what the structure of x is, type:

str(x)

The data we want is in x$band1. To see the values, type:

x$band1

5. The next step is to plot the data. Type

plot(f5.pts$CLAY,x$band1)

Your resulting plot should look like this:

This clearly shows the relationship between clay content and infrared reflectance.

You can do a regression of decIR on CLAY using this commands:

clayir.reg <- lm(x$band1 ~ f5.pts$CLAY)


summary(clayir.reg)

(lm stands for linear model) and you can run a regression line through your plot using

abline(reg = clayir.reg)

Note that R lets you build plots by adding things to them one at a time.
Use the ? command in R to find out what the various functions are doing.
There are number of books that you can use to enhance your knowledge of the application of R to
spatial data. One that I particularly recommend (for an obvious reason) is Spatial Data Analysis
in Ecology and Agriculture Using R by Richard E. Plant (CRC Press).

You might also like