2016-07-25 4 views
1

다양한 공항의 지점과 함께 R의 지리적 그림을 만든 다음 그 사이의 화살표를 사용하여 특정 비행 경로를 나타냅니다.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)) 

당신이 있습니까를 내가 뭘 잘못하고 있는지에 대한 제안?

답변

1

위도와 경도가 혼동되어 세그먼트가 플롯되지 않습니다. x는 위도로, y는 위도로 표시됩니다.

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

감사합니다 : 단지 arrow = arrow()geom_segment에 추가 또한 data = geo를 사용해야하고 화살표를 표시하기 위해, 또한 aes()

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

에서 geo$를 제거! 나는 추가를 시도했다 :'eom_segment (data = geo, arrow (y = lat, x = lon, yend = latend, xend = lonend))' 그러나 그것이 작동하지 않는 것 같다 : ( – MikeR

+0

@MikeR 내 편집 된 답변을 참조하십시오, 그것은 지금 분명해야합니다 :) – beetroot

+0

그 덕분에! 이제 가장 중요한 부분 인 화살을 얻습니다. 화살표를 첫 번째 목적지에서 다른 목적지로 이동하려면 어떻게해야합니까? 내가 내 지리 데이터 집합에 다음 데이터를 추가 말할 수 있습니다 : 'secondrow = C를 (11.10557,60.18959,12.10557,62.18959,999) 지리적 <- 그것이를 생성하지 않는 것처럼 rbind (지리, secondrow)' 보인다 추가 화살표 기본적으로 geom_segment에서 무엇을 변경해야합니까? – MikeR

관련 문제