[英]Spatial data: calculating the distance of points from the maximum point value and plotting
My question is similar to this post where the distance between each point was calculated.我的问题类似于计算每个点之间的距离的这篇文章。
In my case, I am looking to find the distance of each point to the point with the highest value.就我而言,我希望找到每个点到具有最高值的点的距离。 I would also like to plot this relationship with
lm()
, but I am struggling to achieve both tasks with spatial data objects.我也想 plot 这种与
lm()
的关系,但我正在努力用空间数据对象来完成这两项任务。
My data does not need CRS, it is based on the Euclidean distance (because these points are in a room).我的数据不需要CRS,它是基于欧几里得距离(因为这些点在一个房间里)。
A mock example of the data below, where column variable
is of interest.以下数据的模拟示例,其中列
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
>
Turning the data frame into a SpatialPointsDataFrame
with the sp()
package:使用
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
This is the point where I don't know what steps to take, but I want to know the distance of all the points to the highest value:这是我不知道要采取什么步骤的地方,但我想知道所有点到最高值的距离:
which.max(dat@data$variable)
And then plot this relationship with base plot()
.然后 plot 这种关系与基础
plot()
。
If my question is unclear please let me know.如果我的问题不清楚,请告诉我。
I'm still not sure I understand your question but I propose the following answer.我仍然不确定我是否理解您的问题,但我提出以下答案。
Load packages加载包
library(sf)
#> Linking to GEOS 3.9.1, GDAL 3.2.1, PROJ 7.2.1
library(tidyr)
Load data加载数据
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")
)
Separate the xy_coord
column, convert columns to numeric and create an sf object分离
xy_coord
列,将列转换为数字并创建一个 sf object
dat_sf <- st_as_sf(
separate(dat, xy_coord, c("x", "y"), sep = " ", convert = TRUE),
coords = c("x", "y")
)
Find the maximum of variable找到变量的最大值
which.max(dat_sf[["variable"]])
#> [1] 1
Compute all distances计算所有距离
dat_sf[["distances"]] <- st_distance(dat_sf, dat_sf[1, ])
Plot Plot
plot(variable ~ distances, data = dat_sf)
Created on 2021-11-22 by the reprex package (v2.0.1)由代表 package (v2.0.1) 于 2021 年 11 月 22 日创建
You can also remove the first point (with distance = 0).您还可以删除第一个点(距离 = 0)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.