首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用ggmap绘制R中地图上的箭头

如何用ggmap绘制R中地图上的箭头
EN

Stack Overflow用户
提问于 2016-07-25 20:29:10
回答 1查看 1.8K关注 0票数 1

我试图在R中绘制一个地理图,其中包含各个机场的点,然后在它们之间使用箭头,指示特定的飞行路线。

根据前面关于堆栈溢出的一些问题,使用ggmap和geom_segment应该可以做到这一点,但是使用我在下面代码中使用的方法似乎无法工作:

代码语言:javascript
复制
library(htmlwidgets)
library(leaflet)
library(ggmap)


#=============================== Data ===============================#  


#Retrieving coordinates for two airports
Adress = c("Lufthavnsboulevarden 6, 2770 Kastrup, Denmark", "Edvard Munchs veg, 2061 Gardermoen, Norway")

# This function geocodes a location (find latitude and longitude) using the Google Maps API
geo <- geocode(location = Adress, output="latlon", source="google")

#moving the data around, so that second observation will be end-destination
geo$lonend[1] <- geo$lon[2]
geo$latend[1] <- geo$lat[2]

#removing second row
row_to_keep = c(TRUE, FALSE)
geo = geo[row_to_keep,]

#putting a number in there to make some dots for the airports
geo$Number = 999

#=============================== Ploting =============================# 

# get a Google map
require(ggmap)
map<-get_map(location='Europe', zoom=5, maptype = "terrain",
             source='google',color='color')

# plot it with ggplot2
require("ggplot2")
require("RColorBrewer")
ggmap(map) +
  geom_point(
    aes(x=lon, 
        y=lat, 
        show_guide = TRUE, 
        colour=Number), 
    data=geo, 
    alpha=.8, 
    na.rm = T)  + 
  scale_color_gradient(low="beige", high="blue")

#Set the pointer from (y,x) => (yend,xend) using geom-segment
ggmap(map, extent = "device", ylab = "lat", xlab = "lon") + 
  geom_segment(aes(y = geo$lon, x = geo$lat, yend = geo$lonend, xend = geo$latend))

你对我做错了什么有什么建议吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-25 20:36:49

您的线段没有绘制,因为您混淆了纬度和经度,x对应经度,y对应纬度。此外,您应该使用data = geo并从aes()中删除geo$

代码语言:javascript
复制
ggmap(map, extent = "device", ylab = "lat", xlab = "lon") + 
  geom_segment(data = geo, aes(y = lat, x = lon, yend = latend, xend = lonend))

此外,为了显示箭头,只需在geom_segment中添加arrow = arrow()

代码语言:javascript
复制
ggmap(map, extent = "device", ylab = "lat", xlab = "lon") + 
  geom_segment(data = geo, aes(y = lat, x = lon, yend = latend, xend = lonend),
               arrow = arrow())
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38567825

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档