将单个点添加到水平图

时间:2019-10-20 20:40:58

标签: r sp levelplot

我试图帮助一个朋友进行情节,但最终陷入困境。 他想在水平图中绘制样本位置。栅格来自数据包unmarked。 (data(Switzerland))。我已经在一个虚拟数据框上尝试过,我在其中随机选择了一些位置。

我发现了以下问题Add XY points to raster map generated by levelplot,或多或少是我想做的。

所以我尝试了以下代码:

x <- c("980000", "1100000", "1200000")
y <- c("120000", "170000", "100000")
name <- c("a", "b", "c")
dummy <- as.data.frame(cbind(x, y, name))

levelplot(elevation ~ x + y, Switzerland, aspect="iso",col.regions=terrain.colors(100)) +
  layer(sp.points(dummy, cex=2, col=1))

但是我最终收到一条错误消息

Error in .local(obj, ...) : any(sp) is not TRUE

我试图了解sp.points()需要什么样的输入以及我做错了什么,但是失败了。

1 个答案:

答案 0 :(得分:0)

dummy转换为sp对象,即您的情况下的SpatialPointsDataFrame

R> library("sp")
R> dummy <- data.frame(x = as.numeric(x), y = as.numeric(y), name = name)
R> coordinates(dummy) <- ~ x + y
R> class(dummy)
[1] "SpatialPointsDataFrame"
attr(,"package")
[1] "sp"

可复制的示例:

library("sp")
library("lattice")
library("latticeExtra")
library("unmarked")

data(Switzerland)

x <- c(980000, 1100000, 1200000)
y <- c(120000, 170000, 100000)
name <- c("a", "b", "c")

dummy <- data.frame(x, y, name)
coordinates(dummy) <- ~ x + y


p <- levelplot(elevation ~ x + y, Switzerland,
               aspect = "iso", col.regions = terrain.colors(100)) +
     layer(sp.points(dummy, cex = 2, col = 1))
print(p)

levelplot

相关问题