library(sf)
## Linking to GEOS 3.10.2, GDAL 3.4.1, PROJ 8.2.1; sf_use_s2() is TRUE
library(ggplot2)
library(readxl)
library(stringr)
data <- read_excel("/home/taufiq/Documents/freelancer/andi faradilla/data/data_hasil.xlsx")
shapefile_path <- "/home/taufiq/Documents/freelancer/andi faradilla/file peta/BATAS PROVINSI DESEMBER 2019 DUKCAPIL/BATAS_PROVINSI_DESEMBER_2019_DUKCAPIL.shp"
indonesia_map <- st_read(shapefile_path)
## Reading layer `BATAS_PROVINSI_DESEMBER_2019_DUKCAPIL' from data source 
##   `/home/taufiq/Documents/freelancer/andi faradilla/file peta/BATAS PROVINSI DESEMBER 2019 DUKCAPIL/BATAS_PROVINSI_DESEMBER_2019_DUKCAPIL.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 34 features and 4 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XYZ
## Bounding box:  xmin: 95.01079 ymin: -11.00762 xmax: 141.0194 ymax: 6.07693
## z_range:       zmin: 2.65e-05 zmax: 2.65e-05
## Geodetic CRS:  WGS 84
data$PROVINSI <- str_trim(str_to_upper(data$PROVINSI))
indonesia_map$PROVINSI <- str_trim(str_to_upper(indonesia_map$PROVINSI))
indonesia_map_df <- as.data.frame(indonesia_map)
indonesia_map_geometry <- st_geometry(indonesia_map)
indonesia_merged_df <- merge(indonesia_map_df, data, by = "PROVINSI", all.x = TRUE)
indonesia_merged <- st_as_sf(indonesia_merged_df, sf_column_name = "geometry")
st_geometry(indonesia_merged) <- indonesia_map_geometry
indonesia_merged <- st_make_valid(indonesia_merged)
create_map <- function(data, column, breaks, labels, colors, title, subtitle) {
  data$color_bin <- cut(data[[column]], breaks = breaks, labels = labels, include.lowest = TRUE)
  data_centroid <- st_centroid(data)
  
  ggplot(data = data) +
    geom_sf(aes(fill = color_bin), color = "black") +
    geom_sf_text(data = data_centroid, aes(label = PROVINSI), 
                 size = 1, color = "black", check_overlap = TRUE) +
    scale_fill_manual(values = colors, name = "KETERANGAN") +
    labs(title = title, subtitle = subtitle) +
    theme_minimal() +
    theme(legend.position = "bottom")
}
params <- list(
  list(column = "X1", breaks = c(-Inf, 69.41, 69.57, 73.47, Inf), 
       labels = c("< 69.41", "69.41 - 69.57", "69.57 - 73.47", "> 73.47"), 
       colors = c("< 69.41" = "red", "69.41 - 69.57" = "orange", 
                  "69.57 - 73.47" = "blue", "> 73.47" = "green"), 
       title = "Peta Indonesia Berdasarkan Dataset X1", subtitle = "1:3.241.685"),
  
  list(column = "X2", breaks = c(-Inf, 11.88, 11.97, 14.27, Inf), 
       labels = c("< 11.88", "11.88 - 11.97", "11.97 - 14.27", "> 14.27"), 
       colors = c("< 11.88" = "red", "11.88 - 11.97" = "orange", 
                  "11.97 - 14.27" = "blue", "> 14.27" = "green"), 
       title = "Peta Indonesia Berdasarkan Dataset X2", subtitle = "1:3.241.685"),
  
  list(column = "X3", breaks = c(-Inf, 1693.51, 1904.25, 7172.84, Inf), 
       labels = c("< 1693.51", "1693.51 - 1904.25", "1904.25 - 7172.84", "> 7172.84"), 
       colors = c("< 1693.51" = "red", "1693.51 - 1904.25" = "orange", 
                  "1904.25 - 7172.84" = "blue", "> 7172.84" = "green"), 
       title = "Peta Indonesia Berdasarkan Dataset X3", subtitle = "1:3.241.685"),
  
  list(column = "X4", breaks = c(-Inf, 7.85, 7.93, 10.13, Inf), 
       labels = c("< 7.85", "7.85 - 7.93", "7.93 - 10.13", "> 10.13"), 
       colors = c("< 7.85" = "red", "7.85 - 7.93" = "orange", 
                  "7.93 - 10.13" = "blue", "> 10.13" = "green"), 
       title = "Peta Indonesia Berdasarkan Dataset X4", subtitle = "1:3.241.685")
)
for (param in params) {
  print(create_map(indonesia_merged, param$column, param$breaks, param$labels, 
                   param$colors, param$title, param$subtitle))
}
## Warning: st_centroid assumes attributes are constant over 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
## Warning: st_centroid assumes attributes are constant over 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
## Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_centroid assumes
## attributes are constant over 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
## Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_centroid assumes
## attributes are constant over 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