2016-11-27 58 views
-1

我试图在xy.plots上面显示一些变种图模型。 panel.plots除了我想添加到对应的子图的行列表之外。用R格子包画出线条列表

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

http://stackoverflow.com/help/mcve –

+2

如@哈克-R所提到的,请提供一个工作示例。 'meuse'数据位于'library(sp)'FWIW中。 –

+0

@RomanLuštrik完成 – ToNoY

回答

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