# 載入莫名其妙套件
library(sf)
## Linking to GEOS 3.9.3, GDAL 3.5.2, PROJ 8.2.1; sf_use_s2() is TRUE
library(readr)
library(ggplot2)
library(readxl)
library(dplyr)
## 
## 載入套件:'dplyr'
## 下列物件被遮斷自 'package:stats':
## 
##     filter, lag
## 下列物件被遮斷自 'package:base':
## 
##     intersect, setdiff, setequal, union
## 載入工業資料

industry = read_xls("C:/Users/USER/Desktop/工業區環境統計-縣市別_20231113225531150.xls")
data = st_read("C:/Users/USER/Desktop/mapdata202301070205/COUNTY_MOI_1090820.shp")
## Reading layer `COUNTY_MOI_1090820' from data source 
##   `C:\Users\USER\Desktop\mapdata202301070205\COUNTY_MOI_1090820.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 22 features and 4 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 114.3593 ymin: 10.37135 xmax: 124.5611 ymax: 26.38528
## Geodetic CRS:  TWD97
data%>%
  st_crop(xmin=119,xmax=123,ymin=20,ymax=26) %>% 
  ggplot() + 
  geom_sf()
## Warning: attribute variables are assumed to be spatially constant throughout
## all geometries

sf_county_centroid =
  data %>%
  st_centroid(of_largest_polygon = T)
## Warning: st_centroid assumes attributes are constant over geometries
df_coordination = 
  sf_county_centroid %>% 
  st_coordinates()


industry_abs.num = industry[,c(1,2)]
industry_abs.num<- industry_abs.num %>%
  mutate(num = as.numeric(num))


sf_county_industry.num <- 
  data %>% 
  left_join(industry_abs.num, by = c("COUNTYNAME" = "county")) %>%
  mutate(rn = row_number()) %>%
  mutate(x_centroid = df_coordination[,"X"],
         y_centroid = df_coordination[,"Y"])


## 工廠絕對數量台灣地圖
sf_county_industry.num %>%
  st_crop(xmin=119,xmax=123,ymin=20,ymax=26) %>%
  ggplot()+
  geom_sf(size = 0.2, aes(fill = num))+
  geom_sf_text(aes(label = COUNTYNAME), size = 3) +
  scale_fill_gradient(low = "#FADDC3", high = "#EA4C3B", name = '工廠數量')+
  labs(title='台灣工業絕對數量圖', x = '經度', y = '緯度')
## Warning: attribute variables are assumed to be spatially constant throughout
## all geometries
## Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_point_on_surface may not
## give correct results for longitude/latitude data

## 工廠相對數目
industry_abs_relative.num = industry[,c(1,3)]
industry_abs_relative.num<- industry_abs_relative.num %>%
  mutate(num_percent = as.numeric(num_percent))


sf_county_industry_relative.num <- 
  data %>% 
  left_join(industry_abs_relative.num , by = c("COUNTYNAME" = "county")) %>%
  mutate(rn = row_number()) %>%
  mutate(x_centroid = df_coordination[,"X"],
         y_centroid = df_coordination[,"Y"])

sf_county_industry_relative.num %>%
  st_crop(xmin=119,xmax=123,ymin=20,ymax=26) %>%
  ggplot()+
  geom_sf(size = 0.2, aes(fill = num_percent))+
  geom_sf_text(aes(label = COUNTYNAME), size = 3) +
  scale_fill_gradient(low = "skyblue1", high = "slateblue", name = '工廠相對數量')+
  labs(title='台灣工業相對數量圖', x = '經度', y = '緯度')
## Warning: attribute variables are assumed to be spatially constant throughout
## all geometries

## Warning: st_point_on_surface may not give correct results for
## longitude/latitude data

## 工廠佔地面積
industry.area = industry[,c(1,4)]
industry.area<- industry.area %>%
  mutate(area = as.numeric(area))


sf_county_industry.area <- 
  data %>% 
  left_join(industry.area , by = c("COUNTYNAME" = "county")) %>%
  mutate(rn = row_number()) %>%
  mutate(x_centroid = df_coordination[,"X"],
         y_centroid = df_coordination[,"Y"])

sf_county_industry.area %>%
  st_crop(xmin=119,xmax=123,ymin=20,ymax=26) %>%
  ggplot()+
  geom_sf(size = 0.2, aes(fill = area))+
  geom_sf_text(aes(label = COUNTYNAME), size = 3) +
  scale_fill_gradient(low = "#FADDC3", high = "#EA4C3B", name = '工廠佔地面積')+
  labs(title='台灣工業佔地面積圖', x = '經度', y = '緯度')
## Warning: attribute variables are assumed to be spatially constant throughout
## all geometries

## Warning: st_point_on_surface may not give correct results for
## longitude/latitude data

## 工廠佔地面積(趴數)
industry_area.percent = industry[,c(1,5)]
industry_area.percent<- industry_area.percent %>%
  mutate(area_percent = as.numeric(area_percent))


sf_county_industry_area.percent <- 
  data %>% 
  left_join(industry_area.percent , by = c("COUNTYNAME" = "county")) %>%
  mutate(rn = row_number()) %>%
  mutate(x_centroid = df_coordination[,"X"],
         y_centroid = df_coordination[,"Y"])

sf_county_industry_area.percent %>%
  st_crop(xmin=119,xmax=123,ymin=20,ymax=26) %>%
  ggplot()+
  geom_sf(size = 0.2, aes(fill = area_percent))+
  geom_sf_text(aes(label = COUNTYNAME), size = 3) +
  scale_fill_gradient(low = "skyblue1", high = "slateblue", name = '工廠相對數量')+
  labs(title='台灣工業相對數量圖', x = '經度', y = '緯度')
## Warning: attribute variables are assumed to be spatially constant throughout
## all geometries

## Warning: st_point_on_surface may not give correct results for
## longitude/latitude data