使用RandomFields包绘制随机高斯字段的实现结果为空白图。为什么呢?

时间:2017-03-29 13:16:55

标签: r random plot gaussian

由于某些原因,我尝试使用 plot() 函数来显示 RFsimulate() RandomFields函数的输出>包,输出总是空图。

我只是使用帮助文件中包含的示例代码:

## first let us look at the list of implemented models
RFgetModelNames(type="positive definite", domain="single variable",
                iso="isotropic") 

## our choice is the exponential model;
## the model includes nugget effect and the mean:
model <- RMexp(var=5, scale=10) + # with variance 4 and scale 10
  RMnugget(var=1) + # nugget
  RMtrend(mean=0.5) # and mean

## define the locations:
from <- 0
to <- 20
x.seq <- seq(from, to, length=200) 
y.seq <- seq(from, to, length=200)

simu <- RFsimulate(model=model, x=x.seq, y=y.seq)
str(simu)

给出了:

Formal class 'RFspatialGridDataFrame' [package ""] with 5 slots
  ..@ .RFparams  :List of 5
  .. ..$ n         : num 1
  .. ..$ vdim      : int 1
  .. ..$ T         : num(0) 
  .. ..$ coordunits: NULL
  .. ..$ varunits  : NULL
  ..@ data       :'data.frame': 441 obs. of  1 variable:
  .. ..$ variable1: num [1:441] 4.511 2.653 3.951 0.771 2.718 ...
  ..@ grid       :Formal class 'GridTopology' [package "sp"] with 3 slots
  .. .. ..@ cellcentre.offset: Named num [1:2] 0 0
  .. .. .. ..- attr(*, "names")= chr [1:2] "coords.x1" "coords.x2"
  .. .. ..@ cellsize         : Named num [1:2] 1 1
  .. .. .. ..- attr(*, "names")= chr [1:2] "coords.x1" "coords.x2"
  .. .. ..@ cells.dim        : int [1:2] 21 21
  ..@ bbox       : num [1:2, 1:2] -0.5 -0.5 20.5 20.5
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:2] "coords.x1" "coords.x2"
  .. .. ..$ : chr [1:2] "min" "max"
  ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot
  .. .. ..@ projargs: chr NA

...所以数据已被模拟,但是当我打电话时

plot(simu)

我最终得到这样的东西: enter image description here e.g. Empty plot

谁能说出这里发生了什么?!

1 个答案:

答案 0 :(得分:0)

我会将对象强制返回到sp SpatialGridDataFrame并进行绘制,因为RandomFields围绕此S4类创建了一个包装器:

sgdf = sp::SpatialGridDataFrame(simu@grid, simu@data, simu@proj4string)
sp::plot(sgdf)

此外,您可以使用标准图形库强制转换为矩阵并进行绘图:

graphics::image(as.matrix(simu))

奇怪的是,将其转换为SpatialGridDataFrame之前需要进行翻转和转置,然后绘制:

graphics::image(t(apply(as.matrix(sgdf), 1, rev)))

显然,它们在内部有些不一致。最简单的解决方案是将simu转换为rasterplot

r = raster::raster(simu)
raster::plot(r)