Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用ggmap在R中绘制地图上的箭头_R_Google Maps_Plot_Ggmap - Fatal编程技术网

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

如何使用ggmap在R中绘制地图上的箭头,r,google-maps,plot,ggmap,R,Google Maps,Plot,Ggmap,我试图在R中绘制一个地理图,图中有各个机场的点,然后是它们之间的箭头,指示特定的飞行路线 根据前面关于堆栈溢出的一些问题,使用ggmap和geom_段应该可以实现这一点,但使用我在下面代码中的方法似乎不起作用: library(htmlwidgets) library(leaflet) library(ggmap) #=============================== Data ===============================# #Retrieving co

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

根据前面关于堆栈溢出的一些问题,使用ggmap和geom_段应该可以实现这一点,但使用我在下面代码中的方法似乎不起作用:

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))
库(htmlwidgets)
图书馆(单张)
图书馆(ggmap)
#========================================================================================================================================================================================================================================================================================================================================================================
#检索两个机场的坐标
地址=c(“丹麦卡斯特鲁普2770LufthavensBoulevarden62770”,“挪威加德默恩2061年Edvard Munchs veg”)
#此函数使用Google Maps API对位置(查找纬度和经度)进行地理编码

geo由于混淆了纬度和经度,所以未绘制线段,x对应经度,y对应纬度。此外,您应该使用
data=geo
并从
aes()中删除
geo$

此外,为了显示箭头,只需将
arrow=arrow()
添加到
geom_段

ggmap(map, extent = "device", ylab = "lat", xlab = "lon") + 
  geom_segment(data = geo, aes(y = lat, x = lon, yend = latend, xend = lonend),
               arrow = arrow())

非常感谢。我尝试添加:
eom_段(data=geo,arrow(y=lat,x=lon,yend=latend,xend=lonend))
但它似乎不起作用:(@MikeR请查看我编辑的答案,现在应该清楚了:)谢谢!现在我得到一支箭,这是最重要的部分。如果我想让箭头从第一个目的地指向另一个目的地,我该怎么办?假设我将以下数据添加到我的geo数据集中:
secondrow=c(11.10557,60.18959,12.10557,62.18959999)geo@MikeR坐标超出绘图限制,因此第二个箭头不显示。将<代码>缩放> <代码>为4,你会看到它。“迈克,我很高兴能帮上忙,请考虑通过点击复选标记接受我的答案:
ggmap(map, extent = "device", ylab = "lat", xlab = "lon") + 
  geom_segment(data = geo, aes(y = lat, x = lon, yend = latend, xend = lonend),
               arrow = arrow())