|
Geography 414/514: Advanced Geographic Data Analysis Exercise 4: Multivariate Plots, Coplots, and Lattice Graphics 1. Introduction Read through the exercise before attempting to complete it. The purpose of this exercise is to explore some of the general multivariate plotting procedures available in R, including maps, mutivariate plots in particular, conditioning plots (coplots), and Trellis/Lattice grapics. This exercise uses a number of "R packages" or libraries of functions, data sets, etc. that must be downloaded and installed from "CRAN" (you will need to be connected to the Internet to do this). In the Windows R Gui, there is a menu choice "Packages" that assists in downloading and installing packages, (see Packages > Install package(s) from CRAN), and there is a similar feature on the Mac. For this exercise, you'll need to install the following packages: sp, maptools, ClassInt, RColorBrewer and lattice. You can check to see if a package has been successfully downloaded and installed by attempting to load the package with the library() function, e.g.
If an error message is produced e.g. Error in library(maptools) : There is no package called 'maptools') then the download and installation has failed. If that's the case, packages may also be downloaded and installed using the command line in the R Gui, as follows:options(CRAN =
"http://cran.us.r-project.org/") # tell R where to look for packages On a Mac, the documentation suggests that this is done a little differently: options(CRAN =
"http://cran.us.r-project.org/") # tell R where to look for packages You will get the following message: --- Please select a CRAN mirror for use in this session --- and a scrolling list box should open. It turns out that the closest repository to us is in Seattle and is the last one in the list, so scroll down and select it, and then click on "ok". You can also use the Packages menu to chooses the closest mirror.(You don't need to use the command line approach if you use the menu--just download the packages once.) Occasionally, it's a good idea to check if packages have been updated; this can be done by typing.
or using the menu, Packages > Update packages from CRAN. After downloading and installing, it's ok to delete the installation files when prompted. Further information can be found in the FAQs:
2. Simple maps Begin by constructing some simple maps of the Oregon climate-station data [orstationc.csv]. (See Section 2 of Exercise 3 for directions on how to download and save these data if they are no longer in your working directory.) Also, before starting, download and save in your working directory the following components of a shape file of Oregon county outlines: You will find it most helpful to use a text editor for this exercise. TextPad (recommended, it's on the Duckware CD), Notepad or even Word will do the job.
class <- classIntervals(plotvar, nclr, style="quantile") colcode <- findColours(class, plotclr) cutpts <- round(class$brks, digits=1) Print plotvar, plotclr, colornum, brks and colorcode to get an idea of what the above block of code does. # Block 3: plot the shape file and the
selected variable The first block of code should be executed once. The three library() function calls load the sp, maptools, RColorBrewer package . The readShapeLines() function from maptools reads in the shape file. The second block of code sets (a) the variable to plot by assigning, in this case, pann, to plotvar (remember you can discover the names of the variables in the data frame orstationc using the names() function), and (b) the number of colors to use. (To plot other variables, replace pjan in this example.) The remainder of this second block figures out which particular color to assign to each observation.The third block of code plots the shapefile, adds color-coded points, and a legend. To generate a map of an individual variable, one would edit the second block of code, and then execute the second and third blocks, by cutting or pasting. Construct maps for several of the climate variables ( pjan and tjul in particular).
3. The coplot Coplots (or conditioning plots) are probably the most commonly used multipanel plot, and an easy-to-use function is available. The following code creates a coplot for annual temperature, plotted as a function of elevation, given latitude and longitude:
Each panel on the diagram shows the relationship between annual average temperature and elevation for a geographical subset of data. The "panel functions" in coplot() allow lowess curves and least-squares lines to be added to the plot to facilitate interpretation. The individual panels, arranged as they are here, form a map of scatter diagrams.
Experiment with the number of coplot shingles and degree of overlap of each (the number and overlap arguements in the coplot() function.
4. Lattice plots This set of plots and analyses use a data set of glacial-cirque locations in Oregon collected by Deb Sea several years ago for a class project. The data set may be found here: [cirques.csv] and the data can be read into the R workspace after downloading as follows:
The variables are an index number ( Cirque), location (Lat, Lon), elevation in meters (Elev), a region indicator (which can be plotted to discover what the regions are; Region) and a 0/1 variable that indicates whether each cirque is occupied by a glacier (Glacier = 1) or not.(Should you wish to also do nicer maps of these data, here are the links to the appropriate shapefile components: [.dbf] [.shp] [.shx]) The aim of the analysis is to examine the spatial variations in the elevations of the cirque basins, in order to infer what might be controlling their distribution. From theory, we might expect that the "glaciation threshold" or elevation at which glaciers may form, should be lower where it is cooler and moister, and higher where it is warm and dry. The question that can be asked here is whether the Oregon cirque distributions conform to this idea, based on examining that distribution.
panel.xyplot(etc.) and panel.abline(etc.)
|