인구비례 선거구

인구비례 선거구 지도가 필요한 시점이 되었습니다.

저자
소속

1 데이터셋

코드
library(tidyverse)
library(rvest)
library(httr)
library(sf)
library(geogrid)
sf_use_s2(FALSE)

precinct_sf <- st_read("data/2020_21_elec_253.json")

sido_sf <- precinct_sf |> 
  # filter(SGG_1 == "부산") |> 
  calculate_grid(grid_type="hexagonal", learning_rate = 0.05, seed=20)

plot(sido_sf)

sido_hex <- assign_polygons(precinct_sf, sido_sf)
코드
# sido_hex |> 
#   st_write("data/precinct_21.geojson")

sido_hex <- 
  st_read("data/precinct_21.geojson")
#> Reading layer `precinct_21' from data source 
#>   `D:\tcs\map_challenge\data\precinct_21.geojson' using driver `GeoJSON'
#> Simple feature collection with 253 features and 12 fields
#> Geometry type: POLYGON
#> Dimension:     XY
#> Bounding box:  xmin: 125.9417 ymin: 33.08693 xmax: 130.9774 ymax: 38.53052
#> Geodetic CRS:  WGS 84

sido_hex |> 
  st_geometry() |> 
  plot()

2 시도 경계

코드
sido_boundary_hex <- sido_hex |> 
  # ungroup() |> 
  group_by(SGG_1) |> 
  summarise(geometry = st_union(geometry))

sido_boundary_hex |> 
  ggplot() +
    geom_sf(aes(geometry = geometry, fill = SGG_1))

3 SVG to geojson

코드

svg_shp <- st_read("data/precinct_21/2020_South_Korean_legislative_election_Results_Cartogram-polygon.shp")
#> Reading layer `2020_South_Korean_legislative_election_Results_Cartogram-polygon' from data source `D:\tcs\map_challenge\data\precinct_21\2020_South_Korean_legislative_election_Results_Cartogram-polygon.shp' 
#>   using driver `ESRI Shapefile'
#> Simple feature collection with 269 features and 1 field
#> Geometry type: POLYGON
#> Dimension:     XY
#> Bounding box:  xmin: 31.94769 ymin: 33.43049 xmax: 65.61502 ymax: 38.91111
#> Geodetic CRS:  WGS 84

svg_shp |> 
  plot()

4 svg to geojson

코드
mapbox_sf <- st_read("data/precinct_21/features.geojson") 
#> Reading layer `features' from data source 
#>   `D:\tcs\map_challenge\data\precinct_21\features.geojson' using driver `GeoJSON'
#> Simple feature collection with 269 features and 2 fields
#> Geometry type: POLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -374.5006 ymin: 87.79933 xmax: -340.8279 ymax: 89.10986
#> Geodetic CRS:  WGS 84

south_korea_bbox <- st_bbox(c(
  xmin = 124.61,  # Minimum longitude
  ymin = 33.10,   # Minimum latitude
  xmax = 131.87,  # Maximum longitude
  ymax = 38.45    # Maximum latitude
), crs = st_crs(4326))  # EPSG:4326 coordinate reference system

mapbox_sf |>
  st_set_crs(4326) |> 
  # st_crop(south_korea_bbox) |> 
  ggplot() +
    geom_sf(aes(geometry = geometry))