空间数据:计算点距最大点值的距离并作图

Spatial data: calculating the distance of points from the maximum point value and plotting

我的问题类似于此post,其中计算了每个点之间的距离。

在我的例子中,我正在寻找每个点到具有最高值的点的距离。我也想用 lm() 绘制这种关系,但我正在努力用空间数据对象来完成这两项任务。

我的数据不需要CRS,它是基于欧氏距离的(因为这些点在一个房间里)。

以下数据的模拟示例,其中第 variable 列很重要。

> dput(dat)
structure(list(date.hour = structure(c(1551057840, 1551057840, 
1551057840, 1551057840, 1551057840, 1551057840, 1551057840), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), id = c(2, 5, 7, 8, 9, 10, 11), variable = c(456, 
27, 130, 116, 92, 141, 145), xy_coord = c("6.2 14.8", "8.2 8.9", 
"4.2 8.9", "2.2 8.9", "8.2 3.5", "6.2 3.5", "4.2 3.5")), row.names = c(NA, 
-7L), groups = structure(list(id = c(2, 5, 7, 8, 9, 10, 11), 
    date.hour = structure(c(1551057840, 1551057840, 1551057840, 
    1551057840, 1551057840, 1551057840, 1551057840), tzone = "UTC", class = c("POSIXct", 
    "POSIXt")), .rows = structure(list(1L, 2L, 3L, 4L, 5L, 6L, 
        7L), ptype = integer(0), class = c("vctrs_list_of", "vctrs_vctr", 
    "list"))), row.names = c(NA, -7L), class = c("tbl_df", "tbl", 
"data.frame"), .drop = TRUE), class = c("grouped_df", "tbl_df", 
"tbl", "data.frame"))

> dat
# A tibble: 7 x 4
# Groups:   id, date.hour [7]
  date.hour              id variable xy_coord
  <dttm>              <dbl>    <dbl> <chr>   
1 2019-02-25 01:24:00     2      456 6.2 14.8
2 2019-02-25 01:24:00     5       27 8.2 8.9 
3 2019-02-25 01:24:00     7      130 4.2 8.9 
4 2019-02-25 01:24:00     8      116 2.2 8.9 
5 2019-02-25 01:24:00     9       92 8.2 3.5 
6 2019-02-25 01:24:00    10      141 6.2 3.5 
7 2019-02-25 01:24:00    11      145 4.2 3.5 
> 

使用 sp() 包将数据框变成 SpatialPointsDataFrame

#Split x and y to separate columns 
dat$x <- sapply(strsplit(as.character(dat$xy_coord), " "), "[", 1); dat$x <- as.numeric(dat$x)
dat$y <- sapply(strsplit(as.character(dat$xy_coord), " "), "[", 2); dat$y <- as.numeric(dat$y)

#SpatialPointsDataFrame
coordinates(dat) <- ~x+y

这是一个点,我不知道要采取什么步骤,但我想知道所有点到最高值的距离:

which.max(dat@data$variable)

然后绘制与基数 plot() 的关系。

如果我的问题不清楚,请告诉我。

我仍然不确定我是否理解你的问题,但我提出以下答案。

加载包

library(sf)
#> Linking to GEOS 3.9.1, GDAL 3.2.1, PROJ 7.2.1
library(tidyr)

加载数据

dat = structure(
  list(
    date.hour = structure(
      c(
        1551057840, 1551057840, 1551057840, 1551057840, 1551057840, 
        1551057840, 1551057840
      ), 
      tzone = "UTC", 
      class = c(
        "POSIXct",
        "POSIXt"
      )
    ), 
    id = c(2, 5, 7, 8, 9, 10, 11), 
    variable = c(
      456, 27, 130, 116, 92, 141, 145
    ), 
    xy_coord = c(
      "6.2 14.8", "8.2 8.9", "4.2 8.9", "2.2 8.9", "8.2 3.5", "6.2 3.5", 
      "4.2 3.5"
    )
  ), 
  row.names = c(NA,-7L), 
  groups = structure(
    list(
      id = c(2, 5, 7, 8, 9, 10, 11),
      date.hour = structure(
        c(
          1551057840, 1551057840, 1551057840, 1551057840, 1551057840, 
          1551057840, 1551057840
        ), 
        tzone = "UTC", 
        class = c(
          "POSIXct",
          "POSIXt"
        )
      ), 
      .rows = structure(
        list(1L, 2L, 3L, 4L, 5L, 6L, 7L), 
        ptype = integer(0), 
        class = c(
          "vctrs_list_of", "vctrs_vctr", "list"
        )
      )
    ), 
    row.names = c(NA, -7L), 
    class = c("tbl_df", "tbl", "data.frame"), 
    .drop = TRUE
  ), 
  class = c("grouped_df", "tbl_df", "tbl", "data.frame")
)

分离xy_coord列,将列转换为数字并创建一个sf对象

dat_sf <- st_as_sf(
  separate(dat, xy_coord, c("x", "y"), sep = " ", convert = TRUE), 
  coords = c("x", "y")
)

求变量的最大值

which.max(dat_sf[["variable"]])
#> [1] 1

计算所有距离

dat_sf[["distances"]] <- st_distance(dat_sf, dat_sf[1, ])

情节

plot(variable ~ distances, data = dat_sf)

reprex package (v2.0.1)

创建于 2021-11-22

您也可以删除第一个点(距离 = 0)。