R combined gps-track plot of spatial intensity

08/30/2011

To get a quick impression about the temporal stay of places it is helpful to generate a plot of the trackpoints spatial density (intensity).

combined plot of spatial intensity

Spatial intensity 2D/3D

As the 3d visualisation has both advatages and disadvantages, a combination with a 2D plot is useful to interpret the data. The data used in this example is a gps record of the “everyday life” of a test person.

Code snippet:

###################################################
## 3d/2D Plot density with spatstat density.ppp
###################################################

bb_utm <- qbbox(lat = tripdata_utm_num[,2], lon = tripdata_utm_num[,1]) #Boundingbox
Rect <- owin(c(bb_utm$lonR[1]-500, bb_utm$lonR[2]+1800), c(bb_utm$latR[1]-500, bb_utm$latR[2]+500))
P_all <- ppp(tripdata_utm_num[,1], tripdata_utm_num[,2], window=Rect)   #  opt: marks=datetime
PPP_all <- as.ppp(P_all)
den <- density(PPP_all, sigma = 70)

cutted_den <- den  # Wertebereich abschneiden für Extreme
cutvalue <- 0.0020   # Erfahrungswert
cutted_den$v[cutted_den$v>=cutvalue] <-cutvalue

png(paste(plotpath,proband,”intensity_overview_spatstat_v3.png”, sep=””),2400,1200);  # Plot Dichte 3D
layout(matrix(c(1,2), 2, 2, byrow = TRUE), widths=c(1,1))
persp(cutted_den , col=”grey”, d=1, ticktype=”detailed”, cex.lab=4, cex.axis=4, zlim=c(range(cutted_den$v)[1],cutvalue), phi=30, theta=-20, xlab=”longitude”, ylab=”latitude”, zlab=”density”, main=paste(proband, “Räumliche Intensität”))
couleurs  <- tail(topo.colors(trunc(1.4 * 100)),100)
couleurs[1] <- “#0400ff”
plot(cutted_den , col=couleurs, ticktype=”detailed”, xlab=”longitude”, ylab=”latitude”, cex.lab=5, cex.axis=5, main=paste(proband, “Räumliche Intensität 2D”))
#points(SP_UTM@coords[,1], SP_UTM@coords[,2], cex=0.05, col=”grey”)
points(SP_wp_UTM@coords[,1], SP_wp_UTM@coords[,2], cex=2, col=”red”)
text(SP_wp_UTM@coords[,1], SP_wp_UTM@coords[,2], labels=daten_wp$wpOpen, cex=1, adj=c(0,-1), col=”red”)

dev.off()

# If you use any code, please refer to or cite this site (see about geolabs)

6 Responses to “R combined gps-track plot of spatial intensity”


  1. can you provide the data used for this example?

    • Benedikt Orlowski Says:

      I’m afraid but the data could disclose the identity of my test person. The data has been collected in a study about unemployment and its impact on everyday life. But you can use any trackdata of regular gps-trackers. Export the recorded gpx-data to a csv sheet and reed the matrix into R.

  2. NB Says:

    Hi,
    this part is not working

    png(paste(plotpath,proband,”intensity_overview_spatstat_v3.png”,
    sep=””),2400,1200); # Plot Dichte 3D

    what are plotpath and proband?

    NB

    • Benedikt Orlowski Says:

      Plotpath and proband are variables that are merged by the command paste and contain a path where the png should be saved.

  3. Victor Says:

    Hello Benedikt,

    I’ve been working with your script, however I have some questions.

    – in the part png(paste(plotpath,proband,”intensity_overview_spatstat_v3.png”,
    sep=””),2400,1200); # Plot Dichte 3D

    I replaced “plotpath” and “proband” with “C:Rproject” (where Rproject is the folder where I want to save the graph). Is that OK to do it in that way?

    – when I run the part
    points(SP_wp_UTM@coords[,1], SP_wp_UTM@coords[,2], cex=2, col=”red”)
    text(SP_wp_UTM@coords[,1], SP_wp_UTM@coords[,2], labels=daten_wp$wpOpen, cex=1, adj=c(0,-1), col=”red”)

    I don’t know what kind of data I have to replace instead of SP_wp_UTM. Is the same data as “tripdata_utm_num”?

    thanks for advance.

    • Benedikt Orlowski Says:

      The variables plotpath and proband can be replaced with a regular path name in quotation marks. That is correct. Your second question I am not able to answer for 100% sure. As far as I remember, the data SP_wp_UTM consists of the named waypoints plotted above the density plot. You may just delete this part as you probably wont use waypoints for labeling the places.


Leave a reply to NB Cancel reply