# Week 2 Handling Spatial Data 0316
> 大綱:
> 認識GISTools這個library的功能
> Mapping spatial objects & data attributes (主題地圖)
>
## 1. calculate population density
兩種資料形式: 1. Spatial____dataframe 2. data.frame
## 設置圖形的函數:par()中的參數介紹
* mar=c(btm,left,top,right) 圖形邊界設定
* mfrow=c(rows,columns) 可以設定有幾個row跟column,用途在一次呈現多個圖片
library(rgdal)
<span class="red">**讀取shapefile**</span>
readOGR(dsn=路徑,layer=圖檔名稱,encoding=編碼方式)
class:
* spatial points dataframe
* spatial polygons dataframe
factor: 類別因子
1. 類別因子是有level的
將factor轉成character再轉成numeric比較好處理資料
利用 as.character & as.numeric
2. 變更factor的level:
1. ordered(factor, levels = c())
2. factor(factor, levels = c())
確認**投影**座標系統:
proj4string(Taipei_Vill)
OUTPUT:
"+proj=tmerc +lat_0=0 +lon_0=121 +k=0.9999 +x_0=250000 +y_0=0 +ellps=GRS80 +units=m +no_defs"
補充:地理座標系統的EPSG代碼
* TWD97(121): 3826 (250000,2765778)
* WGS84: 4326 (121,25)
* osm()的地理座標系統與投影 WGS84/Pseudo-Mercator: 3857 (13469658,2875745)
tmerc:橫麥卡托
k:縮尺係數
ellps:標準橢球體
units:單位(公尺)
poly.areas(polygon) 計算面積
人口密度=人口數 / 面積
Taipei_Vill$density <- Taipei_Vill$CENSUS / Taipei_Vill$area
## 2. Read spatial data and merge attribute
```r=
Taipei_Vill.f <- fortify(Taipei_Vill, region="VILLAGE")
```
polygon 由轉折點構成
fortify(要讀的資料,region=要保留分類的欄位) 將shapefile物件轉為data.frame
將每個轉折點拆開來,每個轉折點皆為一筆資料,資料量變多,但剛剛人口密度的欄位沒有在上面!!所以還要另外join
### merge(x, y, by.x, by.y): merge two data frames
Taipei_Vill.f <- merge(<span class="red">Taipei_Vill.f</span>, Taipei_Vill@data, <span class="red">by.x = "id"</span>, by.y = "VILLAGE")
by: 利用兩筆資料相同的欄位進行join
```r=
map.0<- ggplot() +
geom_polygon(data =Taipei_Vill.f, aes(x=long, y = lat, group = group), fill="brown", color="white") + coord_fixed(1.0)
map.x <- ggplot(Taipei_Vill.f, aes(long, lat, group = group)) + geom_polygon(fill="brown", color="white")+ coord_fixed(1.0)
```
**group: 把同區的polygon的group起來**
## 4. plotting points
SpatialPointDataframe 中的可能會有兩種座標,一個來自圖形本身內建的,另一個會在dataframe裡面
要叫出圖形本身的座標要用 @coords
x = @coords[,1]
y = @coords[,2]
### Map overlay
```r=
map.0 + geom_point(shape=21, aes(x=coor_x,y=coor_y), fill="yellow", color="black", size=3, stroke = 1 )
```

## 5. plotting graduate-color polygons 面量圖
```r=
map.1 <- ggplot(Taipei_Vill.f, aes(long, lat, group = group, **fill = density**)) +
geom_polygon() + coord_equal() +
labs(x = "x-coor", y = "Y-coor", fill = "Population Density") +
ggtitle("Taipei Population")
```

```r=
library(RColorBrewer)
library(classInt)
density<-Taipei_Vill$density
brks <- classIntervals(density, n = 6, style = "quantile")
map.1 + scale_fill_continuous(low = "white", high = "black")
```

```r=
map.1 + scale_fill_gradientn(colours = brewer.pal(6, "YlGn"), breaks = c(brks$brks), name = "popn.density")
```

## 6. plotting graduate-color/size points
```r=
FFdata<-FastFood@data
map.2<- ggplot() + geom_polygon(data =Taipei_Vill.f, aes(x=long, y = lat, group = group), fill="gray", color="white") + coord_fixed(1.0)
map.2 + geom_point(data=FFdata, aes(x=X_COOR, y=Y_COOR, size=TYPE_99), color="orange")+ scale_size_continuous(name="Sales",range = c(1, 3))
```

```r=
map.2 + geom_point(data=FFdata, aes(x=X_COOR, y=Y_COOR, color=STORE),size=2) + scale_color_manual(values=c("red","blue"))
```

```r=
map.2 + geom_point(data=FFdata, aes(x=X_COOR, y=Y_COOR, size=TYPE_99, color=STORE))+ scale_size_continuous(name="Sales",range = c(1, 3)) + scale_color_manual(values=c("red","blue"))
```

## Plotting spatial objects
地圖要素:
1. 圖名: title()
2. 方向標 north.arrow(x, y, 寬度, 顏色)
3. 比例尺 map.scale(x, y, 長度, 副標, 區塊數, 分段基準)
4. 面量圖圖例 choro.legend(x, y, 分類)