Applied Spatial Data

*Import necessary packages

library(ggplot2)
library(tidyr)
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
library(raster)
## Loading required package: sp
## 
## Attaching package: 'raster'
## The following object is masked from 'package:dplyr':
## 
##     select
## The following object is masked from 'package:tidyr':
## 
##     extract
library(rgdal)
## rgdal: version: 1.2-8, (SVN revision 663)
##  Geospatial Data Abstraction Library extensions to R successfully loaded
##  Loaded GDAL runtime: GDAL 2.0.1, released 2015/09/15
##  Path to GDAL shared files: C:/Program Files/R/R-3.4.1/library/rgdal/gdal
##  Loaded PROJ.4 runtime: Rel. 4.9.2, 08 September 2015, [PJ_VERSION: 492]
##  Path to PROJ.4 shared files: C:/Program Files/R/R-3.4.1/library/rgdal/proj
##  Linking to sp version: 1.2-5
library(sp)

*Obtain Vietnam map (vector data) from GADM website

Vietnam<-getData("GADM", country="Vietnam",level=1)

head(Vietnam@data)
##   OBJECTID ID_0 ISO  NAME_0 ID_1                NAME_1 HASC_1 CCN_1 CCA_1
## 1        1  250 VNM Vietnam    1        Ðà N<U+1EB5>ng  VN.DA    NA   501
## 2        2  250 VNM Vietnam    2       Ð<U+1ED3>ng Nai  VN.DN    NA   713
## 3        3  250 VNM Vietnam    3      Ð<U+1ED3>ng Tháp  VN.DT    NA   803
## 4        4  250 VNM Vietnam    4              Ðak Nông  VN.DO    NA   606
## 5        5  250 VNM Vietnam    5 Ð<U+1EAF>k L<U+1EAF>k  VN.DC    NA   605
## 6        6  250 VNM Vietnam    6      Ði<U+1EC7>n Biên  VN.DB    NA   302
##                                                  TYPE_1
## 1 Thành ph<U+1ED1> tr<U+1EF1>c thu<U+1ED9>c t<U+1EC9>nh
## 2                                           T<U+1EC9>nh
## 3                                           T<U+1EC9>nh
## 4                                           T<U+1EC9>nh
## 5                                           T<U+1EC9>nh
## 6                                           T<U+1EC9>nh
##                     ENGTYPE_1 NL_NAME_1            VARNAME_1
## 1 City|Municipality|Thanh Pho           Da Nang City|Da Nang
## 2                    Province                       Dong Nai
## 3                    Province                      Dong Thap
## 4                    Province                       Dac Nong
## 5                    Province                Dak Lak|Dac Lac
## 6                    Province                      Dien Bien

*A spatial polygon dataframe behaves like a normal dataframe. It is also noted that there are few duplicates in the dataset

# Kin Giang and Ninh Binh provinces are having 
Vietnam@data[duplicated(Vietnam@data$VARNAME_1),]
##    OBJECTID ID_0 ISO  NAME_0 ID_1     NAME_1 HASC_1 CCN_1 CCA_1
## 34       34  250 VNM Vietnam   33 Kiên Giang  VN.KG    NA   813
## 44       44  250 VNM Vietnam   42  Ninh Bình  VN.NB    NA   117
##         TYPE_1 ENGTYPE_1 NL_NAME_1  VARNAME_1
## 34 T<U+1EC9>nh  Province           Kien Giang
## 44 T<U+1EC9>nh  Province            Ninh Binh

*Extract coordinate points of Vietnam to use in ggplot2

# Using ggplot2 to derive the coordinate 

vn<-fortify(Vietnam,regions="VARNAME_1")  # You can pass ID_1 or any other unique variables which are dinstint
## Regions defined for each Polygons
head(vn)
##       long      lat order  hole piece id group
## 1 107.9137 16.21422     1 FALSE     1  1   1.1
## 2 107.9139 16.21408     2 FALSE     1  1   1.1
## 3 107.9140 16.21396     3 FALSE     1  1   1.1
## 4 107.9141 16.21377     4 FALSE     1  1   1.1
## 5 107.9141 16.21355     5 FALSE     1  1   1.1
## 6 107.9142 16.21323     6 FALSE     1  1   1.1

*Visualizing the Vietnam map

# A traditional way of visualization of spatial data

plot(Vietnam,main="Vietnam Map", col=4, axes=T)

# Using a btter way of plotting the map, ggplot2 

ggplot(data=vn, aes(x=long,y=lat,group=group)) + geom_point(fill="blue")

*Create a random variable called GPA

set.seed(123) # Make sure that it is fixed

vn$GPA<-runif(nrow(vn),1,100) # GPA ranges from 1 to 100

head(vn)
##       long      lat order  hole piece id group       GPA
## 1 107.9137 16.21422     1 FALSE     1  1   1.1 29.470174
## 2 107.9139 16.21408     2 FALSE     1  1   1.1 79.042208
## 3 107.9140 16.21396     3 FALSE     1  1   1.1 41.488715
## 4 107.9141 16.21377     4 FALSE     1  1   1.1 88.418723
## 5 107.9141 16.21355     5 FALSE     1  1   1.1 94.106261
## 6 107.9142 16.21323     6 FALSE     1  1   1.1  5.510093

*Visualizing the GPA distribution across Vietnam provinces

ggplot(data=vn, aes(x=long,y=lat,group=group)) + geom_polygon(aes(fill=id),color="gray40",show.legend = F) + labs(title="Vietnam Map",x="Longitude",y="Latitude") + theme(plot.title = element_text(hjust = 0.5))

ggplot(data=vn, aes(x=long,y=lat,group=group)) + geom_polygon(aes(fill=GPA),color="gray40",show.legend = T) + labs(title="Vietnam Map",x="Longitude",y="Latitude") + theme(plot.title = element_text(hjust = 0.5)) + scale_fill_gradient(name="Vietnam GPA",limits=c(1,100),low="green",high="red")

library(viridis)
## Loading required package: viridisLite
ggplot(data=vn, aes(x=long,y=lat, group=group,fill=GPA)) + geom_polygon(color="black") + scale_fill_viridis(direction = 1,option = "viridis") + labs(x="Longitude",y="Latitute", title="GPA Distribution by Province",caption="Tuyen Ha Van, Massey University, NZ") + theme(plot.title = element_text(hjust=0.5))