[R-sig-Geo] Multiple variograms in one graph

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Fri Sep 4 11:18:16 CEST 2009


And here is a solution using the ggplot2 package. Which I find more transparannt than lattice. You have to prepare your data a bit more.

library(gstat)
library(ggplot2)
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 ggplot2 plot
# add the semivariance values of v2 to v1
v1$gammaV2 <- v2$gamma
Fitted <- data.frame(dist = seq(0.01, max(v1$dist), length = 101))
Fitted$gamma <- variogramLine(m1, dist_vector = Fitted$dist)$gamma
Fitted$gammaV2 <- variogramLine(m2, dist_vector = Fitted$dist)$gamma
#convert the dataframes to a long format
Empirical <- melt(v1, id.vars = "dist", measure.vars = c("gamma", "gammaV2"))
Modeled <- melt(Fitted, id.vars = "dist", measure.vars = c("gamma", "gammaV2"))

#one plot for each variogram
ggplot(Empirical, aes(x = dist, y = value)) + 
		geom_point() + 
		geom_line(data = Modeled) + 
		facet_wrap(~variable)

#both variogram on the same plot with different colours
ggplot(Empirical, aes(x = dist, y = value, colour = variable)) + 
		geom_point() + 
		geom_line(data = Modeled)
#same as above but with custom ranges of the X and Y axis
ggplot(Empirical, aes(x = dist, y = value, colour = variable)) + 
		geom_point() + 
		geom_line(data = Modeled) + 
		scale_x_continuous(limits = c(0, max(Empirical$dist))) + 
		scale_y_continuous(limits = c(0, max(c(Empirical$value, Fitted$value))))

HTH,

Thierry 


----------------------------------------------------------------------------
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest
Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium
tel. + 32 54/436 185
Thierry.Onkelinx at inbo.be
www.inbo.be

To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey
 
-----Oorspronkelijk bericht-----
Van: r-sig-geo-bounces at stat.math.ethz.ch [mailto:r-sig-geo-bounces at stat.math.ethz.ch] Namens Paul Hiemstra
Verzonden: vrijdag 4 september 2009 9:58
Aan: Edzer Pebesma
CC: R-sig-Geo at stat.math.ethz.ch
Onderwerp: Re: [R-sig-Geo] 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:
> ageel bushara wrote:
>   
>> Dear all,
>>
>> Â
>>
>> I’m a new user of R. I
>> have two plots of variograms. Each plot shows the experimental 
>> variograms with its fitted theoretical variograms. I would like to 
>> have the two plots in one graph (all experimental and theoretical 
>> variograms will be in one graph). How can I do it?
>>   
>>     
> easiest to use basic plot:
>
> library(gstat)
> 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)) plot(gamma~dist, v2, 
> ylim = c(0, 1.05*max(v2$gamma)),col='red', ylab = 'semivariance', xlab 
> = 'distance') lines(variogramLine(m2, 1500), col='red') 
> points(gamma~dist, v1, col='blue') lines(variogramLine(m1, 1500), 
> col='blue')
>
>   
>> Â
>>
>> Thanks,
>>
>> Ageel
>>
>>
>>
>>
>>
>>
>>
>>       
>> 	[[alternative HTML version deleted]]
>>
>>   
>> ---------------------------------------------------------------------
>> ---
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at stat.math.ethz.ch
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>   
>>     
>
>   


--
Drs. Paul Hiemstra
Department of Physical Geography
Faculty of Geosciences
University of Utrecht
Heidelberglaan 2
P.O. Box 80.115
3508 TC Utrecht
Phone:  +3130 274 3113 Mon-Tue
Phone:  +3130 253 5773 Wed-Fri
http://intamap.geo.uu.nl/~paul

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo at stat.math.ethz.ch
https://stat.ethz.ch/mailman/listinfo/r-sig-geo

Druk dit bericht a.u.b. niet onnodig af.
Please do not print this message unnecessarily.

Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer 
en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is
door een geldig ondertekend document. The views expressed in  this message 
and any annex are purely those of the writer and may not be regarded as stating 
an official position of INBO, as long as the message is not confirmed by a duly 
signed document.


More information about the R-sig-Geo mailing list