Q1.台北捷運各站通車時間與進出站人次散布圖
查核點:
1. 點顏色依路線分成六類(請用近似官方的顏色)
2. 大小依照出口數量顯示(連續)
3. x軸請注意日期的格式
4. y軸取對數,並參考範例列出特定人次的標籤
5. 圖例依通車時間的順序排列,並列出路線名稱(如圖)
6. 標題、座標軸與圖例名稱
setwd("C:/Users/user/Desktop/SA/期中出題")
rm(list=ls())
library(ggplot2)
library(GISTools)
library(spdep)
library(rgdal)
library(ggplot2)
library(reshape2)
#資料匯入與處理
Taipei_Vill= readOGR(dsn=getwd() , layer = "Taipei_Vill", encoding="utf8")
MRT = readOGR(dsn =getwd(), layer = "MRT", encoding="utf8")
Taipei_Vill@data$CENSUS=as.numeric(as.character(Taipei_Vill@data$CENSUS))
MRT@data$VALUE=as.numeric(as.character(MRT@data$VALUE))
MRT@data$EXIT=as.numeric(as.character(MRT@data$EXIT))
MRT@data$OPENTIME=strptime(as.character(MRT@data$OPENTIME),format='%Y%m%d')
MRT@data$LINE=factor(MRT@data$LINE,order=TRUE,levels = c("TRANS","BR","R","G","O","BL"))
MRT@data$EXIT
md=MRT@data
names(md)
#Q1
ggplot(md)+geom_point(aes(x=OPENTIME,y=VALUE,color=LINE,size=EXIT),alpha=0.8)+
scale_y_log10("進出站人次/年(人次)",breaks=c(2000000,5000000,10000000,20000000,50000000,100000000),labels=c("2百萬","5百萬","1千萬","2千萬","5千萬","1億"))+
scale_color_manual(name="捷運站點所在路線\n(依通車時間排序)",
values= c("TRANS"="#666666","BR"="#c48c31","R"="#e3002c","G"="#008659","O"="#f8b61c","BL"="#0070bd"),
labels=c("轉運站","文湖線","淡水信義線","松山新店線","中和新蘆線","板南線"))+
scale_size_continuous(name="出口數量",breaks=c(2,4,6,8,10),range = c(3,9))+
labs(x="通車時間(年)",title ="台北捷運各站通車時間與進出站人次散布圖")+ theme_light() +theme(plot.title = element_text(size=14,hjust = 0.5))
Q2.台北市各行政區內捷運站個數
查核點:
1. 加總計算需正確
2. 以顏色代表不同路線(以疊加方式繪製長條圖)
3. x軸行政區,y軸捷運站個數(標籤沒有小數點)
4. 標題、座標軸與圖例名稱
#Q2
Taipei_Town=gUnaryUnion(Taipei_Vill,Taipei_Vill@data$TOWN)
town_names=names(Taipei_Town)
linename=c("TRANS","BR","R","G","O","BL")
MA=matrix(NA,nrow=6,ncol=12)
for (i in 1:6){
MA[i,]=as.vector(poly.counts(subset(MRT,LINE==linename[i]),Taipei_Town))
}
a=data.frame(name=town_names,num=MA[1,],RD=linename[1])
b=data.frame(name=town_names,num=MA[2,],RD=linename[2])
c=data.frame(name=town_names,num=MA[3,],RD=linename[3])
d=data.frame(name=town_names,num=MA[4,],RD=linename[4])
e=data.frame(name=town_names,num=MA[5,],RD=linename[5])
f=data.frame(name=town_names,num=MA[6,],RD=linename[6])
town_data=rbind(a,b,c,d,e,f)
ggplot(town_data,aes(x=name,y=num,fill=RD))+
geom_bar(stat='identity',position = "stack") +
scale_y_continuous(breaks=c(0:10),labels = c(0:10))+
scale_fill_manual("捷運站點所在路線",
values= c("TRANS"="#666666","BR"="#c48c31","R"="#e3002c","G"="#008659","O"="#f8b61c","BL"="#0070bd"),
labels=c("轉運站","文湖線","淡水信義線","松山新店線","中和新蘆線","板南線"))+ theme_minimal()+
labs(title ="台北市各行政區內捷運站個數", x = "行政區", y = "捷運站個數")+
theme(plot.title = element_text(size=16,hjust = 0.5))
Q3.台北市核心區各里人口密度與捷運站點地圖
定義「核心區」:「大安、大同、中山、中正、松山、信義、萬華」七個行政區
(在鄉鎮市區之尺度下,此七區人口密度高於臺北市平均,故定義為核心區)查核點:
1. 只包含核心區底圖和點位
2. 底圖以里繪製人口密度圖
3. 顯示行政區的邊框
4. 點顏色依路線分成六類
5. 點大小依進出口人次繪製,在圖例列出特定標籤(如圖)
6. 標題、與圖例名稱
#Q3
sel=c("大安區","大同區","中山區","中正區","松山區","信義區","萬華區")
Taipei_SET=subset(Taipei_Vill,TOWN %in% sel)
Taipei_SET$area<- poly.areas(Taipei_SET)/10^6
Taipei_SET@data$dens=Taipei_SET@data$CENSUS/Taipei_SET$area
Taipei_SET.f <- fortify(Taipei_SET, region="VILLAGE")
Taipei_SET.f <- merge(Taipei_SET.f, Taipei_SET@data, by.x = "id", by.y = "VILLAGE")
Taipei_MER.f <- fortify(Taipei_SET, region="TOWN")
Taipei_MER.f <- merge(Taipei_MER.f, Taipei_SET@data, by.x = "id", by.y = "TOWN")
MRT_TPE=MRT[Taipei_SET,]
md_tpe=MRT_TPE@data
ggplot()+
geom_polygon(data =Taipei_SET.f, aes(x=long, y = lat, group = group,fill=dens) , color="#888888") +
geom_polygon(data =Taipei_MER.f, aes(x=long, y = lat, group = group),fill=rgb(0,0,0,0), color="black") +
geom_point(data=md_tpe,aes(x=XCOOR,y=YCOOR,color=LINE,size=VALUE))+coord_equal()+
scale_fill_gradientn("人口密度(人/每平方公里)\n",colors=brewer.pal(7,"YlOrRd"))+
scale_size_continuous(name="年進出口人次",breaks=c(1e+7,2e+7,5e+7),labels=c("一千萬","兩千萬","五千萬"),range = c(2,10))+
scale_color_manual(name="站點所在路線",values= c("TRANS"="#3A0088","BR"="#c48c31","R"="#e3002c","G"="#008659","O"="#f8b61c","BL"="#0070bd"),labels=c("轉運站","文湖線","淡水信義線","松山新店線","中和新蘆線","板南線"))+
labs(title ="台北市核心區各里人口密度與捷運站點地圖")+
theme_void()+
theme(plot.title = element_text(size=16,hjust = 0.5))