[英]Calculate points at intervals between two coordinates in R
I have a series of co-ordinates from which I have sampled data, here is an example of it on a 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))
Here is the image this produces: 这是产生的图像:
I want to draw lines between the north and south, and east and west points on the map - to make a sort of plus sign. 我想画一张南北之间的线条,以及地图上的东西点 - 制作一种加号。
BUT I want this line to comprise points rather than just be a line, so I need to calculate the coordinates of the points. 但是我希望这条线包含点而不仅仅是一条线,所以我需要计算点的坐标。 The lines are approximately thirty metres and I require a point every metre.
线约三十米,每米需要一个点。 I feel like the sp package might allow something like this but I can't figure out how.
我觉得sp包可能允许这样的东西,但我无法弄清楚如何。
Thanks, 谢谢,
Kez KEZ
Rearrange your your dataframe; 重新排列您的数据框; all you really need is
seq
. 所有你真正需要的是
seq
。 Here Hadley-style, but build as manually as you prefer: 这里是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))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.