※圖資:
* MRT.shp:台北市捷運站點資料(TWD97-TM2)
MRT_NAME (MRT_ID):捷運站名稱(捷運站編號)
LINE:捷運站路線分類(單一路線/轉運站)
* TPE_LI.shp:台北市村里面資料(WGS84-經緯度)
VILLAGE (V_ID):村里(村里編號)
CENSUS:人口數(單位:人)※注意資料格式
* SCHOOL.shp:台北市中學學校點資料(TWD97-TM2)
NAME (ENG_NAME):消防隊的單位名稱(英文名稱)
TYPE:學校類別(國中/高中/高職)※座標參考系統 CRS 之 proj4 格式:
[EPSG:4326] WGS84 經緯度:
+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84
[EPSG:3826] TWD97 TM2:
+proj=tmerc +lat_0=0 +lon_0=121 +k=0.9999 +x_0=250000+y_0=0 +ellps=GRS80 +units=m +no_defs
*初步環境建置與讀取檔案
library(GISTools);library(rgdal);library(sp);library(ggplot2) setwd("D:/1072SA/Mid1/Mid1_Data") windowsFonts(JH = windowsFont("微軟正黑體")) MRT=readOGR(dsn = ".", layer = "MRT", encoding="unicode",verbose = F) TPE=readOGR(dsn = ".", layer = "TPE_LI", encoding="unicode",verbose = F) SCHOOL=readOGR(dsn = ".", layer = "SCHOOL", encoding="unicode",verbose = F) TPE=spTransform(TPE,MRT@proj4string)
一、 [20%] 地圖繪製:利用ggplot套件繪製台北市人口密度與捷運站點分布
TPE$CENSUS=as.numeric(as.character(TPE$CENSUS)) TPE$AREA=poly.areas(TPE) TPE$DENSITY=TPE$CENSUS/TPE$AREA*10^3 TPE.f = fortify(TPE, region="V_ID") TPE.f = merge(TPE.f, TPE@data, by.x = "id", by.y = "V_ID")
簡易版
ggplot()+ geom_polygon(data =TPE.f, aes(x=long, y = lat, group = group,fill=DENSITY)) + geom_point(aes(x=MRT@coords[,1],y=MRT@coords[,2],color=MRT$LINE),pch=19)+coord_fixed(1.0)+ scale_color_discrete("LINE",labels=c("板南線","文湖線","松新線","中蘆線","淡信線","轉運站"))
完整版
ggplot()+ geom_polygon(data =TPE.f, aes(x=long, y = lat, group = group,fill=DENSITY), color="black") + scale_fill_gradientn("人口密度\n(千人/km2)",colors=brewer.pal(7,"Reds"))+ geom_point(aes(x=MRT@coords[,1],y=MRT@coords[,2],color=MRT$LINE),pch=19,cex=3)+coord_fixed(1.0)+ scale_color_manual("捷運路線",values=c("BL"="#0070bd","BR"="#c48c31","G"="#008659","O"="#f8b61c","R"="#e3002c","TRANS"="purple"),labels=c("BL"="板南線","BR"="文湖線","G"="松新線","O"="中蘆線","R"="淡信線","TRANS"="轉運站"))+ geom_point(aes(x=MRT@coords[,1],y=MRT@coords[,2]),color="#444444",pch=1,cex=3)+ scale_x_continuous(labels =NULL,name =NULL )+ scale_y_continuous(labels =NULL,name =NULL )+ theme_void()+labs(title ="台北人口密度與捷運地圖")+ theme(legend.position = "right")+theme(plot.title = element_text(size=16,hjust = 0.5))+ theme(text=element_text(family="JH"))
二、 [50%] 資料分析:估計捷運站鄰近地區的人口數
- [30%] 計算台北市捷運站方圓500公尺涵蓋的總人口數。
MRTB=gBuffer(MRT,width=500) Inter=gIntersection(MRTB,TPE,byid=T) ID=as.numeric(substring(names(Inter),8,10)) IPOP=TPE$CENSUS[ID+1] IA1=poly.areas(Inter) IA2=TPE$AREA[ID+1] sum(round(IPOP*IA1/IA2))
## [1] 1179439
- [20%] 建立自訂函數,提供使用者設定某捷運站在特定距離方圓內涵蓋的人數。
#2-2 STN_POP=function(id,dist){ STN=MRT[MRT$MRT_ID==id,] STNB=gBuffer(STN,width=dist) Inter=gIntersection(STNB,TPE,byid=T) IDs=as.numeric(substring(names(Inter),8,10))+1 pop=TPE$CENSUS[IDs]*poly.areas(Inter)/TPE$AREA[IDs] return(sum(round(pop))) }
STN_POP (38,500)
## [1] 6729
STN_POP (20,1000)
## [1] 102791
STN_POP (48,1000)
## [1] 93584
STN_POP (43,2000)
## [1] 359245
三、 [30%] 圖表繪製:台北市國中到最近捷運站距離的累積頻率曲線
JH=SCHOOL[SCHOOL$TYPE=="國中",] MJ.d=gDistance(JH,MRT,byid=T) MJ.n=sort(apply(MJ.d,2,min)) MJ.i=c(1:length(MJ.n))/length(MJ.n) par(family="JH") plot(MJ.n,MJ.i,'l',col="blue",main="國中至最近捷運站距離的累積曲線",xlab="距離(公尺)",ylab="學校比例",xaxs = "i", yaxs ="i") points(MJ.n,MJ.i,pch=20,col="blue")