User Tools

Site Tools



R is a program which is constructed from libraries, you need certain libraries for certain types of analysis To add a library type

library(library name)

for example
library(rcmdr) Launches R Commander GUI
commander() relaunches R Commander GUI

Using R with GRASS GIS

Open a mapset
To start R type:


Firstly you need the library with allows R to access the GRASS data: spgrass6


to check it has worked use


This will list various information about your current mapset
Even though you are working in R you can access GRASS commands using the system command


This will bring up the g.list GUI

Reading Vector Data

To access data from GRASS you need to get R to create a SpatialGridDataFrame class object, in this case we call the DataFrame data

data <- readVECT6(c("mapA"),plugin=F)

use summary() to see check it has imported correctly


This will give you:

  • Minimum
  • 1st Quartile
  • Median
  • Mean
  • 3rd Quartile
  • Maximum

for each of your columns in the attribute table

You can also plot the data using the plot() function


to get a nicer plot of your data use


Reading Raster Data

Raster data is imported in a similar way as vector data just by using the readRAST6 function


Visualising data

There are many ways to visualise data

Bars, lines and pies

This section will demonstrate how to create nice looking graphs from your data.


Also these come with many options to change the defaults:
graph layout
main the title of the graph …main=“Main Title”…
xlim the x axis limits …xlim=c(1,10)…
ylim the y axis limits …ylim=c(1,100)…
xlab the label for the x axis …xlab=“x axis label”…
ylab the label for the y axis …ylab=“y axis label”…
xaxt x axis type …xaxt=“n”… other options:
yaxt y axis type …yaxt=“n”… other options:

colours and symbols

More than one graph at a time
Some times you want to see a selection of graphs at the same time, R is able to do this through the par command
par(mfrow=c(1,1)) this will set the environment to display one graph
par(mfrow=c(1,2)) this will set 1 row 2 columns
par(mfrow=c(2,2)) this will give a 2 by 2 appearance

you only need to do this once then the next graph you create will be in the first position and any following graphs will follow after.

Removing data

Sometimes there is data which you do not want to display. newdata←data[data>=1] This will remove Zeros and minus numbers, usefull if you want to present numbers of artefacts ignoring when there are none found in an area.

length(data) this gives the amount of items in your dataset
data[2] selects the 2nd row
data[-2] all except the 2nd row
data[1:5] the first 5 rows
data[(length(x)-5):length(x)] last 5 rows
data[c(1,3,5)] exact rows
data[x>3] values greater than 3
data[ x< -2 | x > 2] greater or less than some values
which(data == max(data)) identifies which is the maximum value

Spatial Analysis

Global Analysis

Local Analysis

Getis and Ord's Gi*

Developed in 1992, this analysis helps identify clusters. It is part of a group of analyses collectivly called LISA (Local Indicators of Spatial Association). It is known as Local G, Gi* or GiStar, it can be found in the spdep library.
localG(x, listw, zero.policy=NULL, spChk=NULL) see LocalG

Significance testing

Hierarchical Cluster Analysis (HCA)

Here we look at the Agnes method (Agglomerative Nesting ), this treats very observation as a single cluster (one artefact clusters at a distance of zero). The two closest clusters are then combined, then the next two closest are combined until only a single cluster (the dataset) remains.

Other types are:

Hierarchical methods: Agnes, Diana, and Mona.

Partitioning methods: Pam, Clara, and Fanny.

mydata <- read.table(file="mydata.txt", header=TRUE,row.names=1, sep=",")
mydata.agnes <- agnes(mydata)
mydata.agnes.euc <- agnes(mydata, metric="euclidean",method="ward", stand=TRUE)


The text file you read into the mydata dataframe must be a matrix in the following form:

A 2542
B2 751
C57 98
D459 1

To do this in GRASS…

Alternatively ArcGIS with Hawths tools and the sampling tools for a NxN matrix, this saves as a .csv but just change it to a .txt

Methods “average” ([unweighted pair-]group average method, UPGMA)
“single” (single linkage)
“complete” (complete linkage)
“ward” (Ward's method)
“weighted” (weighted average linkage) and its generalization
“flexible” which uses (a constant version of) the Lance-Williams formula and the par.method argument
Default is “average”

grass_r/start.txt · Last modified: 2018/08/04 00:01 (external edit)