Skip to content

Commit

Permalink
Create hex_create.R
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonjb82 committed Apr 19, 2019
1 parent db70ec9 commit 5020b85
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions PRU14/hex_create.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Load libraries
library(rgdal)
library(ggplot2)
library(grid)
library(geogrid)
library(maptools)
library(viridis)
library(readxl)
library(stringr)
library(tidyr)
library(gridExtra)
library(tigris)
library(scales)
library(extrafont)

# Set working directory
setwd("D:/R/DataViz-R/PRU14")

# Read shapefile into R
input_file <- "D:/R/DataViz-R/PRU14/malaysia_par_2015_demographics/perak.shp"

original_shapes <- read_polygons(input_file)

raw <- read_polygons(input_file)
raw@data$xcentroid <- sp::coordinates(raw)[,1]
raw@data$ycentroid <- sp::coordinates(raw)[,2]

clean <- function(shape) {
shape@data$id = rownames(shape@data)
shape.points = fortify(shape, region="id")
shape.df = merge(shape.points, shape@data, by="id")
}

# Function to create hexagon bins from shapefile
par(mfrow = c(2, 4), mar = c(0, 0, 2, 0))
for (i in 1:8) {
new_cells <- calculate_grid(shape = raw, learning_rate = 0.01, grid_type = "regular", seed = i)
plot(new_cells, main = paste("Seed", i, sep = " "))
}

new_cells_hex <- calculate_grid(shape = raw, grid_type = "regular", seed = 4)
resulthex <- assign_polygons(raw, new_cells_hex)

writeOGR(obj=resulthex, dsn="D:/R/DataViz-R/PRU14", layer="perak_parl_hex", driver="ESRI Shapefile")

0 comments on commit 5020b85

Please sign in to comment.