gpt4 book ai didi

r - 计算R中两个坐标之间间隔的点

转载 作者:行者123 更新时间:2023-12-02 01:25:01 25 4
gpt4 key购买 nike

我有一系列坐标,我从中采样了数据,这是 map 上的一个例子:

library(ggplot2)
library(ggmap)
library(dismo)
lon <- c(-64.723946, -64.723754, -64.723808, -64.724004)
lat <- c(32.350843, 32.350783, 32.350618, 32.350675)
df <- as.data.frame(cbind(lon,lat))

mapgilbert <- get_map(location = c(lon = mean(df$lon), lat = mean(df$lat)), zoom = 18, maptype = "satellite", scale = 2)
ggmap(mapgilbert) +
geom_point(data = df, aes(x = lon, y = lat, fill = "red", alpha = 0.8), size = 5, shape = 21) +
guides(fill=FALSE, alpha=FALSE, size=FALSE) +
scale_x_continuous(limits = c(-64.725, -64.723), expand = c(0,0)) +
scale_y_continuous(limits = c(32.350, 32.351), expand = c(0,0))

这是生成的图像:

map

我想在 map 上的南北点和东西点之间画线 - 做一个加号。

但是我希望这条线包含点而不仅仅是一条线,所以我需要计算点的坐标。线大约三十米,我需要每米一个点。我觉得 sp 包可能允许这样的事情,但我不知道怎么做。

谢谢,

凯兹

最佳答案

重新排列你的数据框;您真正需要的只是seq。这里是 Hadley 风格,但可以根据需要手动构建:

library(dplyr)
library(tidyr)

# group rows by opposite pairs
df2 <- df %>% group_by(group = lon %in% range(lon)) %>%
# summarize lat and lon for each group into a list of a sequence from the first to the second
summarise_each(funs(list(seq(.[1], .[2], length.out = 10)))) %>%
# expand list columns with tidyr::unnest
unnest()

head(df2)
# Source: local data frame [6 x 3]
#
# group lon lat
# (lgl) (dbl) (dbl)
# 1 FALSE -64.72395 32.35084
# 2 FALSE -64.72393 32.35082
# 3 FALSE -64.72392 32.35079
# 4 FALSE -64.72390 32.35077
# 5 FALSE -64.72388 32.35074
# 6 FALSE -64.72387 32.35072

# all I changed here is data = df2
mapgilbert <- get_map(location = c(lon = mean(df$lon), lat = mean(df$lat)), zoom = 18, maptype = "satellite", scale = 2)
ggmap(mapgilbert) +
geom_point(data = df2, aes(x = lon, y = lat, fill = "red", alpha = 0.8), size = 5, shape = 21) +
guides(fill=FALSE, alpha=FALSE, size=FALSE) +
scale_x_continuous(limits = c(-64.725, -64.723), expand = c(0,0)) +
scale_y_continuous(limits = c(32.350, 32.351), expand = c(0,0))

plot with x of points

关于r - 计算R中两个坐标之间间隔的点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37691511/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com