【问题标题】:How can I add directional arrows to lines drawn on a map in R?如何将方向箭头添加到在 R 中的地图上绘制的线条?
【发布时间】:2015-04-04 19:36:38
【问题描述】:

我有一张使用mapsgeosphere 包构建的地图,看起来像航空公司地图。但是,我想在线条上添加箭头以显示地图中“路线”的方向。您可以在下面看到我当前的工作代码(基于来自FlowingData 的精彩教程)。我之前尝试过使用箭头功能代替线条功能,但我不确定如何使箭头与地圈曲线一致,或者确保箭头沿线间隔开,以便它们看起来像这样:

-->-->-->

我对 R 非常陌生,因此我们将不胜感激任何和所有帮助。提前致谢。

library(maps)
library(geosphere)
read.csv("http://marsiccr.github.io/Data/airports.csv", header=TRUE, as.is=TRUE) -> airports
read.csv("http://marsiccr.github.io/Data/leaders.csv", header=TRUE, as.is=TRUE) -> flights
pal <- colorRampPalette(c("#f2f2f2", "blue"))
colors <- pal(100)
colleges<-NULL
colleges$name <- airports$insname
colleges$long <- airports$long
colleges$lat <- airports$lat
colleges
map("state")
map("state", col="#f2f2f2", fill=TRUE, bg="white", lwd=0.25)
fsub <- flights[flights$type == "aau",]
fsub <- fsub[order(fsub$cnt),]
maxcnt <- max(fsub$cnt)
for (j in 1:length(fsub$type)) {
  air1 <- airports[airports$unitid == fsub[j,]$school1,]
  air2 <- airports[airports$unitid == fsub[j,]$school2,]
  inter <- gcIntermediate(c(air1[1,]$long, air1[1,]$lat), c(air2[1,]$long, air2[1,]$lat), n=100, addStartEnd=TRUE)
  colindex <- round( (fsub[j,]$cnt / maxcnt) * length(colors) )
  lines(inter, col=colors[colindex], lwd=0.8)
}

【问题讨论】:

  • 代码中的 url 返回 404 not found,能否请您提供一个新的、可重现的示例?这会有所帮助

标签: r dictionary maps data-visualization geosphere


【解决方案1】:

inter&lt;- 得到我的箭头(和一些警告)之后将此代码滑入 for 循环

tinter <- tail(inter,2)
arrows(tinter[1,1], tinter[1,2], tinter[2,1], tinter[2,2]) 

显然需要进行一些调整。有关全部选项,请参阅?arrows。您还可以使用inter 矩阵中的倒数第二个(或倒数第五个?)点。您可能还只想为选定的路线添加箭头。

【讨论】: