繁体   English   中英

如何使用R连接地图的所有点

[英]How to connect all the points of a map using R

我想制作一张显示点的地图,并将每个点与其他点连接起来 ,例如网络 我知道我必须使用某种循环或组合,但是我在最后一步进行了堆叠。 拜托,谢谢您的帮助。 提前致谢!

setwd("C:/data")

# Plot a simple map of the Spanish-French border without any detail
xlim <- c(-3.36, 4.78)
ylim <- c(40.17, 44.63)
map1 <- map("world", col="#f2f2f2", fill=TRUE, bg="white", lwd=0.05, xlim=xlim, ylim=ylim)

# Plot nodes
locations <- read.csv("locations.csv", sep=";")
symbols(locations$longitude, locations$latitude, bg="#e2373f", fg="#ffffff", lwd=0.5, circles=rep(1, length(locations$longitude)), inches=0.05, add=TRUE)

# Plot links [here is where I need your HELP!], the following code do not achieve my goal...
for (i in 2: length(locations$longitude)-1) {
  lngs <- c(locations$longitude[i], locations$longitude[i+1])
  lats <- c(locations$latitude[i], locations$latitude[i+1])
  lines(lngs, lats, col="#e2373f", lwd=2)

这是一种尝试(我尚未测试此代码):

connectTheDots <- function(index) {
  remainingIndices <- (index + 1):nrow(locations)
  for (j in remainingIndices) {
    lngs <- c(locations[index, "longitude"], locations[j, "longitude"])
    lats <- c(locations[index, "latitude"], locations[j, "latitude"])
    lines(lngs, lats, col="#e2373f", lwd=2)
  }
}

for (i in 1:nrow(locations)) {
  connectTheDots(i)
}

# this is essentially a nested for loop,
# but I tried to make the syntax simpler by making a function first

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM