簡體   English   中英

空間數據:計算點與最大點值的距離並繪圖

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

我的問題類似於計算每個點之間的距離的這篇文章

就我而言,我希望找到每個點到具有最高值的點的距離。 我也想 plot 這種與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() package 將數據幀轉換為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 這種關系與基礎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 object

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

plot(variable ~ distances, data = dat_sf)

代表 package (v2.0.1) 於 2021 年 11 月 22 日創建

您還可以刪除第一個點(距離 = 0)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM