用R晶格包绘制线列表

时间:2016-11-27 06:47:08

标签: r lattice sp gstat

我试图在 xy.plots 的顶部显示一些变异函数模型。除了我想要添加到相应子图的行列表之外, panel.plots 适用于所有行。

require(gstat)
require(sp)
data(meuse)
names(meuse)
#make directional variograms
b<-variogram(log(zinc)~1, meuse, alpha = c(0, 45, 90, 135))
#split the variogram data by direction
a<-lapply(1:length(unique(b$dir.hor)), 
          function(i) subset(b, grepl(unique(b$dir.hor)[[i]], b$dir.hor)))
#get the model fit parameters for each directional variogram
a<-lapply(1:length(unique(b$dir.hor)),
          function(i) fit.variogram(a[[i]], vgm(0.5,"Exp", 1200, 0.5)))
#generate model data for the directional variograms
a<-lapply(1:length(a), function(i)
  variogramLine(a[[i]], maxdist=1500))

#plot
require(lattice)
plot(b, ylim=c(0,1.2), xlim=c(0,1500), cex=1.5,
     panel = function(x, y, ...) {panel.xyplot(x, y, ...)
                                  panel.abline(v=1000, lwd=1, lty=3, col=2)
                                  panel.lines(a[[i]], lwd=2, lty=2, col=4)
     })

情节中的最后一个参数不起作用,因为它是列表(参见情节显示中的错误),不确定如何用晶格。帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

使用packet.number()panel.number()查找当前面板的标识。你的例子是失败的,所以我做了一些改变,使它正常工作。

library(gstat)
library(sp)
data(meuse)

# make directional variograms
b <- variogram(log(zinc) ~ x + y, meuse, alpha = c(0, 45, 90, 135))

# split the variogram data by direction
a <- lapply(1:length(unique(b$dir.hor)),
            function(i) subset(b, grepl(unique(b$dir.hor)[[i]], b$dir.hor)))

# get the model fit parameters for each directional variogram
a <- lapply(1:length(unique(b$dir.hor)),
            function(i) fit.variogram(a[[i]], vgm(0.5, "Exp", 1200, 0.5)))

# generate model data for the directional variograms
a <- lapply(1:length(a), function(i) variogramLine(a[[i]], maxdist = 1500))

# plot
require(lattice)
plot(b, ylim = c(0, 1.2), xlim = c(0, 1500), cex = 1.5,
     panel = function(x, y, ...) {
       panel.xyplot(x, y, ...)
       panel.abline(v = 1000, lwd = 1, lty = 3, col = 2)
       panel.lines(a[[packet.number()]], lwd = 2, lty = 2, col = 4)
     })

Imgur