The packages spatstat and sp are two of the most important packages containing functions specifically designed for visualisation and analysis of spatial point pattern data. If you do not have these packages installed, then you can get them by typing
install.packages("spatstat", dependencies = TRUE) install.packages("spatstat.utils", dependencies = TRUE) install.packages("sp", dependencies = TRUE)
or in windows, by using the ‘point and click’ equivalent.
We load these packages into R using
library(sp) library(spatstat) library(spatstat.utils)
Most good R packages include ‘vignettes’, these are documents designed to help users understand and use the main package functions. A list of the available vignettes for these two packages is available from CRAN at
and
note that there are also reference manuals for each of the packages. The reference manual contains details of all R functions in the package; these documents can be very long, for example the spatstat reference manual is over 1000 pages long.
We first simulate random points in the spatial region .
x <- runif(100, 0, 1) y <- runif(100, 0, 2)
We can plot against using
plot(x, y, asp = 1)
Note that asp=1 ensures that one unit on the axis will have the same length as one unit on the axis.
We can create an equivalent ‘planar point pattern’ object, an object of class ppp, in R as follows. Firstly, we create an object, win, containing the observation window. To do this, we use the function owin, which returns an object of class owin; there are many useful methods for objects of this class (for example, there is a plot method).
win <- owin(xrange = c(0, 1), yrange = c(0, 2))
The owin function can be used to create much more complicated observation windows including those comprised of multiple polygons with holes.
We create the ppp object as follows:
pts1 <- ppp(x = x, y = y, window = win) pts1
Planar point pattern: 100 points window: rectangle = [0, 1] x [0, 2] units
You can now use the plot method for ppp objects to plot this pattern: plot(pts1). An alternative way of simulating a completely spatially random point process on the same obsesrvation window is as follows:
pts2 <- runifpoint(100, win)
This is the same as generating the points from a Uniform distribution on the window.
Use the command rpoispp to simulate a third realisation of a completely spatially random point process on the window win with the expected number of points equal to 100. How many points did you simulate?
Now plot the pts1, pts2 and pts3 side by side. These are 3 realisations of complete spatial randomness on the window.
What is the key difference between the realisations pts1, pts2 and pts3?
The definition of the boundary of the region affects the visualisation of the data, and possibly also the conclusions drawn. For example, suppose that instead of the boundaries as defined above, we thought that the correct study region was .
win2 <- owin(xrange = c(0, 5), yrange = c(0, 5)) pts4 <- ppp(x = x, y = y, window = win2)
plot(pts4)
In this case, the data appear as a cluster on the bottom left corner of the region , whereas in reality, they are random points in the spatial region . Extra caution is therefore required in defining the study region.
The function clickpoly() allows you to define manually your own boundary. First make sure that you have a window open, where you have plotted the data:
plot(x, y, xlim = c(-0.5, 1.5), ylim = c(-0.5, 2.5))
Unnumbered Figure: Link
You can then use
poly <- clickpoly(add = TRUE)
to manually define the window. Use the left mouse button to click on the graphics window where you want the vertices of the polygon to be. When you finish, you do not have to click on the first vertex again to join the box, instead you should either click on the middle or right mouse button as indicated and let the program do it for you. Note that if you are using Rstudio, then you might need to press the Esc key to finish your polygon.
To test whether a collection of points lie within a particular polygonal observation window, we can use the function inside.owin. For example, if we define another owin object,
win3 <- owin() # the unit square observation window
then we can use
inwin <- inside.owin(pts1, w = win3)
to test which of the points in the point pattern pts1 are inside the unit square.
Use class(inwin) to find out what class of object this is.
Reproduce the case and control plots from Childhood leukaemia data in Humberside using basic plotting commands. Note that the background to these plots in the notes was produced using the package OpenStreetMap, which we will not cover in this course. Three data sets are needed:
leukaemia_cases.txt, leukaemia_controls.txt and leukaemia_poly.txt
You can find these on Moodle. To read these data into R, use the function read.table.
HINT 1: You will need to learn how to specify a polygonal window. Look at the help file, ?owin.
HINT 2: Use the help file, ?ppp, to find out about the marks argument.
Use the clickpoly and inside.owin functions to extract cases and controls which lie within subregions of your choice.
Read into your R workspace the data-sets
myrtles.diseased.txt and myrtles.healthy.txt
These files give the locations of a number of diseased and healthy myrtle trees in an environmental study.
Plot the locations of the trees on a single figure, using different symbols or colours to distinguish between them. What do you conclude from this figure?