Skip to content
Prev 6398 / 29559 Next

Multiple variograms in one graph

Hi,

...or alternatively using the trellis plot system:

library(gstat)
library(lattice)
data(meuse)
coordinates(meuse)=~x+y
v1 = variogram(log(zinc)~1,meuse)
v2 = variogram(log(cadmium)~1,meuse)
m1 = fit.variogram(v1, vgm(1, "Sph", 800, 1))
m2 = fit.variogram(v2, vgm(1, "Sph", 800, 1))

# Arrange the data for the trellis plot
# add the semivariance values of v2 to v1
v1$gammaV2 = v2$gamma

# Construct the plot
xyplot(gamma + gammaV2 ~ dist, v1,
    panel = function(...) {
       panel.xyplot(..., col = c("red","blue"))
       # First variogram model
       ret = variogramLine(m1, maxdist = max(v1$dist))
       llines(x = ret$dist, y = ret$gamma, col = "red", lty = 1) 
       # Second variogram model
       ret = variogramLine(m2, maxdist = max(v2$dist))
       llines(x = ret$dist, y = ret$gamma, col = "blue", lty = 1)        
    },
    ylab = "semivariance",
    xlab  = "distance")

This is a bit more work than what Edzer suggested, but this solution is 
much more flexible. For example if you have not only two variograms at 
the same time, but also several times at which there are variograms. In 
addition, xyplot takes care of the x and y limits automatically. Like 
often in R, you can do stuff in several different ways.

cheers,
Paul
Edzer Pebesma wrote: