我试图在R中绘制一个地理图,其中包含各个机场的点,然后在它们之间使用箭头,指示特定的飞行路线。
根据前面关于堆栈溢出的一些问题,使用ggmap和geom_segment应该可以做到这一点,但是使用我在下面代码中使用的方法似乎无法工作:
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))你对我做错了什么有什么建议吗?
发布于 2016-07-25 20:36:49
您的线段没有绘制,因为您混淆了纬度和经度,x对应经度,y对应纬度。此外,您应该使用data = geo并从aes()中删除geo$
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()
ggmap(map, extent = "device", ylab = "lat", xlab = "lon") +
geom_segment(data = geo, aes(y = lat, x = lon, yend = latend, xend = lonend),
arrow = arrow())https://stackoverflow.com/questions/38567825
复制相似问题