[問題類型]:
程式諮詢(我想用R 做某件事情,但是我不知道要怎麼用R 寫出來)
[軟體熟悉度]:
請把以下不需要的部份刪除
入門(寫過其他程式,只是對語法不熟悉)
[問題敘述]:
各位大大好,我目前急迫性需要在一個國家地圖上畫出幾個城市的地理位置點
以及畫出連接兩個地理位置的連接線,但線的部份一直無法成功畫出來
,想在此請求幫忙
我使用Shapefile裡的數據,已經將地圖和城市的代碼和地理位置畫出來。
我程式裡有兩個csv檔案,A檔包含該國家的城市代碼以及地理
位置(longitude, latitude)清單,B檔包含哪兩個連接點需要被連接起來
的清單。
因為Shapefile裡面的地理位置數據單位為UTM(Universal Transverse Mercator)
coordinate system,我已在我的codes中將csv A檔的longtitude/latitude轉換為
UTM 單位,城市點也畫出來了,run codes的時候也沒有bugs, 所以我懷疑的是
我下面的codes最後一部份gcintermediate() 那邊可能有地理位置單位的問題以至
系統無法找出UTMcoordinates畫連接線,但我已試非常多不同方法,依然無法
畫出連接線,麻煩大大們的幫忙,會非常感激!
這是目前的OUTPUT結果:
https://www.dropbox.com/s/tvd1chw3e6tyy6y/paint.png?dl=0
[程式]:
# Load necessary packages
library(maps)
library(sp)
library(maptools)
library(geosphere)
library(rgdal)
#library(ggmap)
# Function that returns coordinates of a given location
getCoords<-function(location,df){
return(df[match(x=location,df[,"ResidentID2012"]),][,c(4,3)])
}
# Read in the Shapefile
Map <-readShapeSpatial("County.shp")
# Read CSV file containing coordinates and edges
familylocation2012<-read.csv(file="CityLocation.csv",sep=",",header=TRUE)
networkconnection<-read.csv(file="NodesConnection.csv",sep=",",header=TRUE)
# Plot the graph itself
plot(Map,lwd=0.01,bg="#242424",col="#0000CD")
# Setting existing coordinate as lat-long system
cord.dec <- SpatialPoints(cbind(familylocation2012$Longitude,
familylocation2012$Latitude), proj4string = CRS("+proj=longlat"))
# Transforming coordinate to UTM using EPSG=19938 for WGS=84, UTM Zone=35N
cord.UTM <- spTransform(cord.dec, CRS("+init=epsg:3301"))
# Plot the nodes on the graph
points(cord.UTM, pch=16,cex=1.5, col = "white")
# Label the nodes
text(attr(cord.UTM, "coords")[,1], attr(cord.UTM, "coords")[,2],
familylocation2012$ResidentID2012,cex=1,adj=0,pos=1,col="#C2C2C2")
#Connet points and plot the line
for (j in 1:nrow(networkconnection)){
plotLine(networkconnection[j,]$BEGINPOINT,networkconnection[j,]$ENDPOINT,familylocation2012)
}
# Plot a great circle from 'Begin' to 'Endpoint' names.
plotLine<-function(BEGINPOINT,ENDPOINT,df){
inter<-gcIntermediate(p1=getCoords(BEGINPOINT,df),
p2=getCoords(ENDPOINT,df),
n=50,addStartEnd=TRUE)
lines(inter, col="white",cex=2,lwd=2)
}
[環境敘述]:
> sessionInfo()
R version 3.2.1 (2015-06-18)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 8 x64 (build 9200)
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United
States.1252 LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C LC_TIME=English_United
States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] rgdal_1.0-4 geosphere_1.4-3 maptools_0.8-36 maps_2.3-11
sp_1.1-1
loaded via a namespace (and not attached):
[1] tools_3.2.1 foreign_0.8-63 grid_3.2.1 lattice_0.20-31
> > sessionInfo()
R version 3.2.1 (2015-06-18)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 8 x64 (build 9200)
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United
States.1252 LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C LC_TIME=English_United
States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] rgdal_1.0-4 geosphere_1.4-3 maptools_0.8-36 maps_2.3-11
sp_1.1-1
loaded via a namespace (and not attached):
[1] tools_3.2.1 foreign_0.8-63 grid_3.2.1 lattice_0.20-31
>
[關鍵字]
Graphics, social network, R mapping