如何使用ggmap在R中的地图上绘制箭头
How to plot arrows on map in R with ggmap
我试图在 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))
你对我做错的地方有什么建议吗?
您的线段未被绘制,因为您混淆了纬度和经度,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))
此外,为了显示箭头只需将 arrow = arrow()
添加到 geom_segment
:
ggmap(map, extent = "device", ylab = "lat", xlab = "lon") +
geom_segment(data = geo, aes(y = lat, x = lon, yend = latend, xend = lonend),
arrow = arrow())
我试图在 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))
你对我做错的地方有什么建议吗?
您的线段未被绘制,因为您混淆了纬度和经度,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))
此外,为了显示箭头只需将 arrow = arrow()
添加到 geom_segment
:
ggmap(map, extent = "device", ylab = "lat", xlab = "lon") +
geom_segment(data = geo, aes(y = lat, x = lon, yend = latend, xend = lonend),
arrow = arrow())