Я пытаюсь сделать географический участок в R с точками различных аэропортов, а затем стрелки между ними, указывая конкретный маршрут полета.Как рисовать стрелки на карте в R с помощью ggmap
По некоторым ранее вопросы здесь на переполнение стека, то это должно быть возможно с помощью 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))
У Вас есть любые предложения о том, что я делаю неправильно?
Спасибо! Я попытался добавить: 'eom_segment (data = geo, arrow (y = lat, x = lon, yend = latend, xend = lonend))' , но кажется, что он не работает :( – MikeR
@MikeR см. Мой отредактированный ответ, теперь должно быть понятно :) – beetroot
спасибо за это! Теперь я получаю стрелу, которая является самой важной частью. Что делать, если требуется стрелка, идущая от первого пункта назначения до другого пункта назначения? Допустим, я добавить следующие данные в моем гео наборе данных: 'secondrow = с (11.10557,60.18959,12.10557,62.18959,999) гео <- rbind (гео, secondrow)' Похоже, он не создавая дополнительная стрелка по умолчанию - что мне нужно изменить в geom_segment? – MikeR