#本篇使用老師2012的code再去進行數字的調整
##實際調整只調整民意大於50及小於50來區分KMT與DDP
# 以gvotes 變項所顯示
# 因為僅二分法所以直接對照wiki 分別對照各縣市填入



# install the package first
library(maptools)
## Loading required package: sp
## Checking rgeos availability: TRUE
library(sf)
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(tmap)
library(RColorBrewer)
library(ggplot2)

# TWN_adm2.shp is shape data from the DIVA-GIS
# change the path to where the data folder is unpacked on your computer
tw <- st_read("/Users/Tjlee/Desktop/weather/TWN_adm/TWN_adm2.shp")
## Reading layer `TWN_adm2' from data source `C:\Users\Tjlee\Desktop\weather\TWN_adm\TWN_adm2.shp' using driver `ESRI Shapefile'
## Simple feature collection with 22 features and 11 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: 116.71 ymin: 20.6975 xmax: 122.1085 ymax: 25.63431
## geographic CRS: WGS 84
# variable names
names(tw)
##  [1] "ID_0"      "ISO"       "NAME_0"    "ID_1"      "NAME_1"    "ID_2"     
##  [7] "NAME_2"    "TYPE_2"    "ENGTYPE_2" "NL_NAME_2" "VARNAME_2" "geometry"
# names of administrative unit
tw$NAME_2
##  [1] Kaohsiung City <NA>           Taipei City    Changhwa       Chiayi        
##  [6] Hsinchu        Hualien        Ilan           Kaohsiung      Keelung City  
## [11] Miaoli         Nantou         Penghu         Pingtung       Taichung City 
## [16] Taichung       Tainan City    Tainan         Taipei         Taitung       
## [21] Taoyuan        Yunlin        
## 21 Levels: Changhwa Chiayi Hsinchu Hualien Ilan Kaohsiung ... Yunlin
# percents votes received by the DDP party
# hand code data from wiki
# order by names of administrative unit
# attach the data to map data
tw$gvotes <- c(53.42, NA, 56.54, 56.49, 54.81, 35.21, 25.94, 
          52.53, 53.42, 56.77, 53.18, 42.37, 55.65, 
          55.13, 54.68, 44.68, 57.72, 57.72, 53.46,
          40.50, 55.85, 55.81)

# green or blue by 50 percent votes
gcol <- ifelse(tw$gvotes > 50.0, "green", "blue")

# plot
plot(tw, col=gcol)
## Warning: plotting the first 9 out of 12 attributes; use max.plot = 12 to plot
## all

##