使用RandomFields包绘制随机高斯场的实现导致空白图形。为什么?

3
出于某些原因,当我尝试使用 plot() 函数来可视化 RandomFields 包中的 RFsimulate() 函数的输出时,输出总是一个空图。我只是在使用帮助文件中包含的示例代码。
## 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)

我最终得到了这样的东西: 在此输入图片描述 例如:空白图 有人能告诉我这里发生了什么?!

5
你是否在使用RStudio?我遇到了一个类似的问题,仅限于在RStudio中出现。使用 x11(); plot(simu) 会打开一个新窗口,并正确地绘制出随机场。 - xraynaud
我在Rstudio中遇到了完全相同的问题。@xraynaud的解决方案有效,但并不理想。有什么想法为什么会发生这种情况? - ASeaton
1个回答

0

我会将对象强制转换回一个spSpatialGridDataFrame,并绘制它,因为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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接