由于某些原因,当我尝试使用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)
我最终得到了这样的结果:
有人能说出这里发生了什么吗?!
发布于 2018-08-16 04:32:37
我会强制对象返回到一个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
转换为raster
和plot
r = raster::raster(simu)
raster::plot(r)
https://stackoverflow.com/questions/43094652
复制相似问题