2013-11-26 3 views
4

아메리카 대륙에서 네덜란드로의 수출에 관한지도를 만들고 있습니다. 내 데이터를 시각화하기 위해 미주에서 네덜란드까지의 화살표가있는지도를 만들고 싶습니다. 나는 cshapes 세계지도와 ggplot2를 사용한다.ggplot2의 두 위치 사이에 곡선을 그립니다.

data = data.frame("Country.name" = c("Brazil","USA","Canada","Paraguay","Uruguay"), "lng" =  
c(14.23,37,56.13,-23.44,-32.52), "lat" = c(-51.92,-95.71,-106.34,-58.44,-55.77)) 

require(cshapes) 
cshp.data = cshp(date=as.Date("2012-1-1"), useGW=TRUE) 
region.data.frame = fortify(cshp.data, region = "ISO1AL3") 

ggplot(region.data.frame) + geom_polygon(aes(long,lat,group=group)) + 
geom_segment(data = data, aes(x = lat, y = lng, xend= (5.29 - 0.1 * (5.29 - lat)), yend= (52.13 - 0.1 * (52.13 - lng))), 
arrow=arrow(length=unit(0.5,"cm"), angle = 45, type = "closed")) 

직선을 그릴 때 선이 겹치는 것을 발견했습니다. 이것은 추한 것입니다. 따라서 ggplot2 내의 좌표 사이에 곡선을 그릴 방법을 찾고 있으므로 중복되지 않습니다. 내가 여기에 몇 가지 이유 cshapes 실행되지만 없습니다

+1

아마도 대원 경로 뭔가 될 수 있을까? [geosphere 패키지] (http://cran.r-project.org/web/packages/geosphere/vignettes/geosphere.pdf)를 살펴보십시오. – Henrik

+1

사용 중일 수 있습니다. http://stackoverflow.com/questions/18753863/bend-line-toward-center-of-triangle-ggplot2 –

답변

9

grid 패키지와 ggplot2annotation_custom() 함수에서 curveGrob()를 사용하여 곡선을 구축하는 방법의 예입니다. 그것은 당신에게 많은 유연성을 제공합니다. 추신 : 대부분의 매개 변수는 기본값입니다. 편집 - 2 개의 곡선을 표시하도록 업데이트되었습니다.

require(grid) 
g<-qplot(c(0,10),c(0,10)) 
myCurve<-curveGrob(0, 0, 1, 1, default.units = "npc", 
       curvature = 0.3, angle = 90, ncp = 20, shape = 1, 
       square = FALSE, squareShape = 1, 
       inflect = FALSE, arrow = arrow(), open = TRUE, 
       debug = FALSE, 
       name = NULL, gp = gpar(), vp = NULL) 

myCurve2<-curveGrob(0, 0, 1, 1, default.units = "npc", 
       curvature = -0.3, angle = 60, ncp = 10, shape = 1, 
       square = FALSE, squareShape = 1, 
       inflect = FALSE, arrow = arrow(), open = TRUE, 
       debug = FALSE, 
       name = NULL, gp = gpar(), vp = NULL) 

g + 
    annotation_custom(grob=myCurve,0,10,0,10) + # plot from 0,0 to 10,10 
    annotation_custom(grob=myCurve2,2.5,6,2.5,6) # plot from 2.5,2.5 to 6,6 

#REFERENCE>>http://stat.ethz.ch/R-manual/R-devel/library/grid/html/grid.curve.html 

enter image description here

관련 문제