【问题标题】:Add single points to levelplot将单点添加到 levelplot
【发布时间】:2020-02-17 00:02:46
【问题描述】:

我想帮助一个朋友策划一个阴谋,但最终被卡住了。 他想在水平图上绘制样本位置。栅格来自包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() 需要什么样的输入以及我做错了什么,但失败了。

【问题讨论】:

    标签: r sp levelplot


    【解决方案1】:

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-16
      • 2016-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-01
      相关资源
      最近更新 更多