2017-10-21 61 views
1

我試圖在Mollweide投影的世界地圖上繪製線條失敗。我還在同一張地圖上繪製了點,並且效果很好。對於線條,我試圖將這個例子適應我的需求:http://flowingdata.com/2011/05/11/how-to-map-connections-with-great-circles/。 我已經通過預測試(前4步)失敗。在下面的代碼中,該行應該連接肯尼亞和澳大利亞。它運行時沒有錯誤,但輸出中沒有行。 (我還測試了例子,而不mapproj而行是存在的。)在投影圖上繪製連接線(mapproj,gcIntermediate)

library("maps") 
library("mapproj") 
library("geosphere") 

map("world",proj="mollweide", orientation= c(90,0,0), fill=TRUE, col="white", bg="lightblue") 

lon_ke <- 38 
lat_ke <- 1 

lon_aus <- 133 
lat_aus <- -27 

inter <- gcIntermediate(c(mapproject(lon_ke,lat_ke), proj="mollweide", orientation= c(90,0,0)), 
         c(mapproject(lon_aus,lat_aus), proj="mollweide", orientation= c(90,0,0)), 
         n=50, addStartEnd=TRUE) 
lines(inter) 
+0

我想補充一點,mapproj和gcIntermediate在我在這裏找到的相同代碼中的唯一用法:Rahlf,Thomas。 Data Visualization with R. Springer,2017,doi:10.1007/978-3-319-49751-8,p。 345f。您可以下載腳本[here](http://extras.springer.com/2017/978-3-319-49750-1/scriptsanddata.zip) - > maps_world_great_circles.r。我發現這個例子過於複雜,並且它不適合我,在第14行'映射(proj = myProj.type,orient = myProj.orient,wrap = T):投影失敗了某些數據' – noschmi

回答

0

我找到了解決我的問題的基礎上,托馬斯Rahlf的書(見註釋)。這是我的腳本(它有助於可視化作者發佈文章的位置)。

library(maps) 
library(geosphere) 
library(mapproj) 

#load data 
locations <- read.csv("articles-authors-locations.csv", header=TRUE, check.names = FALSE) 

#plot map with Mollweide projection 
myProj.type<-"mollweide" 
myProj.orient<-c(90,0,0) 
x<-map(proj=myProj.type,orient=myProj.orient,wrap=T,fill=TRUE, col="white", bg="lightblue",xlim=range(locations$ArticleLong),ylim=range(locations$ArticleLat) 
     ) 

#plot jittered points for authors' locations 
myStartP<-mapproject(jitter(locations$AuthorLong,amount=3),jitter(locations$AuthorLat, amount=1),proj=myProj.type,orient=myProj.orient) 
points(myStartP,col="darkblue",pch=20,cex=1) 

#set transparent colors 
myTColour<-rgb(0,0,0,50,maxColorValue=255) 
red_transp <- adjustcolor("red", alpha.f = 0.4) 

#plot lines and jittered points, connecting authors' and articles locations 
for (i in 1:nrow(locations)) 
{ 
myGC1<-gcIntermediate(c(locations$AuthorLong[i],locations$AuthorLat[i]),c(locations$ArticleLong[i],locations$ArticleLat[i]),addStartEnd=T, n=50) 
moll<-mapproject(myGC1[,1],myGC1[,2],projection=myProj.type,orientation=myProj.orient) 
lines(moll$x,moll$y,lwd=2.5,col=myTColour) 
myDestP<-mapproject(
    jitter(locations$ArticleLong[i], amount=3), 
    jitter(locations$ArticleLat[i], amount=1), 
    proj=myProj.type,orient=myProj.orient) 
points(myDestP,col=red_transp,pch=20,cex=1) 
}