2014-12-29 3 views
1

ggmap에 대한 경로 다리를 계획하고 있습니다. 지금까지는 괜찮습니다. 나는 각 다리의 순서 (루프에서 n)를 포함하는 레이블을 추가하려고 시도했습니다.다리를 시작할 때마다지도에 레이블을 추가하십시오.

나는 geom_leg()에 + geom_text을 시도했지만 나는 오류 얻을 : 나는 다리를 표시하는 라벨을 추가 도움을 감사하겠습니다

Error in geom_leg(aes(x = startLon, y = startLat, xend = endLon, yend = endLat), : 
    non-numeric argument to binary operator 

합니다.

데이터 :

structure(c("53.193418", "53.1905138631287", "53.186744", "53.189836", 
"53.1884117", "53.1902965", "53.1940384", "53.1934748", "53.1894004", 
"53.1916771", "-2.881248", "-2.89043889005541", "-2.890165", 
"-2.893896", "-2.88802", "-2.8919373", "-2.8972299", "-2.8814698", 
"-2.8886692", "-2.8846099"), .Dim = c(10L, 2L)) 

기능 :

create.map<-function(lst){ 

library("ggmap") 


cncat<-c(paste(lst[,1],lst[,2],sep=",")) 

df2<-data.frame(cncat) 

leg <-function(start, dest, order){ 

    r<- route(from=start,to=dest,mode = c("walking"),structure = c("legs")) 
    c<- geom_leg(aes(x = startLon, y = startLat,xend = endLon, yend = endLat), 
       alpha = 2/4, size = 2, data = r,colour = 'blue')+ 
    geom_text(aes(label = order), size = 3) 

    return (c) 
} 

a<-qmap('Chester, UK', zoom = 15, maptype = 'road') 

for (n in 1:9){ 
    l<-leg(as.character(df2[n,1]), as.character(df2[n+1,1]),n) 
    a<-a+l 
} 

a 

} 

답변

2

가까운 건가요? (참고 : 귀하의 포인트 목록 way.points).

way.points <- as.data.frame(way.points,stringsAsFactors=FALSE) 
library(ggmap) 
rte.from <- apply(way.points[-nrow(way.points),],1,paste,collapse=",") 
rte.to <- apply(way.points[-1,],1,paste,collapse=",") 
rte  <- do.call(rbind, 
        mapply(route, rte.from, rte.to, SIMPLIFY=FALSE, 
          MoreArgs=list(mode="walking",structure="legs"))) 
coords <- rbind(as.matrix(rte[,7:8]),as.matrix(rte[nrow(rte),9:10])) 
coords <- as.data.frame(coords) 
ggm <- qmap('Chester, UK', zoom = 15, maptype = 'road') 
ggm + 
    geom_path(data=coords,aes(x=startLon,y=startLat),color="blue",size=2)+ 
    geom_point(data=way.points,aes(x=as.numeric(V2),y=as.numeric(V1)), 
      size=10,color="yellow")+ 
    geom_text(data=way.points, 
      aes(x=as.numeric(V2),y=as.numeric(V1), label=seq_along(V1))) 

그래서이 다음 데이터 프레임 rte 좌표의 전체 목록을 반환하는 두 벡터로 route(...) 전화 mapply(...) 사용로부터 apply(...)를 사용하여 좌표의 벡터를 조립한다. 좌표는 예 : $startLat$endLat 인 경우 $endLat$endLong을 에 추가하여 coords 데이터 프레임을 형성하여 경로의 마지막 부분을 가져옵니다. 그런 다음 geom_path(...)을 사용하여 한 단계로 경로를 그립니다. 마지막으로 우리는 원래 way.points 데이터 프레임의 x 및 y 값과 함께 geom_text(...)을 사용하며,이를 두드러지게 보이기 위해 geom_point(...)을 사용합니다.

1

여기에 베어 뼈 솔루션입니다. 방금 완성 된 ggmap 객체 인 a에 레이블을 추가했습니다. 당신이 당신의 create.map 기능에

lst2 <- data.frame(cbind(lst, leg = as.character(1:10)) 
names(lst2) <- c("lat", "lon", "leg") 
a <- a + geom_text(data=lst2,aes(x=lon,y=lat,label=leg),size=5, vjust = 0, hjust = -0.5) 
return(a) 

와 라인

a 

를 교체하는 경우, 당신은 (거의) 원하는 결과를 얻을 수 있습니다. latlon 개의 변수를 뒤집을 수 있으며 크기, 위치 등을 조정하고 싶을 수도 있습니다. 도움이 되었기를 바랍니다.