library(sf)
## Linking to GEOS 3.6.2, GDAL 2.2.3, PROJ 4.9.3
library(ggplot2)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
setwd("~/Dropbox/BGU/1_Other/Siddhartha_Mandal/p_03_India_states_shapefile/")
# Read polygon Shapefile
dat = st_read("India_state_code.shp", stringsAsFactors = FALSE)
## Reading layer `India_state_code' from data source `/home/michael/Dropbox/BGU/1_Other/Siddhartha_Mandal/p_03_India_states_shapefile/India_state_code.shp' using driver `ESRI Shapefile'
## Simple feature collection with 36 features and 2 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: 68.18625 ymin: 6.755953 xmax: 97.41529 ymax: 37.07827
## epsg (SRID): 4326
## proj4string: +proj=longlat +datum=WGS84 +no_defs
# Reshape polygon layer to table
pol = fortify(as(dat, "Spatial"), region = "ST_2code")
pol = rename(pol, ST_2code = id)
pol = left_join(pol, st_set_geometry(dat, NULL), "ST_2code")
head(pol)
## long lat order hole piece ST_2code group
## 1 92.89898 12.91588 1 FALSE 1 AN AN.1
## 2 92.89905 12.91512 2 FALSE 1 AN AN.1
## 3 92.89905 12.91459 3 FALSE 1 AN AN.1
## 4 92.89942 12.91456 4 FALSE 1 AN AN.1
## 5 92.89948 12.91459 5 FALSE 1 AN AN.1
## 6 92.89969 12.91499 6 FALSE 1 AN AN.1
## ST_NM
## 1 Andaman & Nicobar Island
## 2 Andaman & Nicobar Island
## 3 Andaman & Nicobar Island
## 4 Andaman & Nicobar Island
## 5 Andaman & Nicobar Island
## 6 Andaman & Nicobar Island
# Plot with ggplot2
ggplot() +
geom_polygon(
data = pol,
aes(x = long, y = lat, group = group, fill = ST_2code),
alpha = 0.75
)
