[问题类型]:
程式咨询(我想用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