-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.R
146 lines (131 loc) · 4.5 KB
/
map.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
################################################################################
#
# Install and load required libraries if not installed
#
################################################################################
#
# Require maptools, rgeos, rgdal, raster
#
if(!require(maptools, quietly = TRUE)) install.packages("maptools") # If maptools required but not installed, install
if(!require(rgeos, quietly = TRUE)) install.packages("rgeos") # If rgeos required but not installed, install
if(!require(rgdal, quietly = TRUE)) install.packages("rgdal") # If rgdal required but not installed, install
if(!require(raster, quietly = TRUE)) install.packages("raster") # If raster required but not installed, install
#
# install papuanewguinea R package from OMNeoHealth Github; devtools
#
if(!require(devtools, quietly = TRUE)) install.packages("devtools") # If devtools required but not installed, install
install_github("OMNeoHealth/papuanewguinea") # Install OMNeoHealth/papuanewguinea from GitHub
library(papuanewguinea) # Load papuanewguinea package
################################################################################
#
# Learn spatial data structure and practice plotting
#
################################################################################
#
# View spatial data structure of country, province, district, llg
#
country
province
district
llg
#
#
#
country@data
province@data
district@data
llg@data
#
#
#
plot(country)
plot(country, lwd = 2)
plot(country, lwd = 2, border = "red")
plot(country, lwd = 2, border = "red", col = "blue")
plot(country, lty = 2, lwd = 2, border = "red", col = "blue")
plot(province)
plot(province, lwd = 2)
plot(province, lwd = 2, border = "red")
plot(province, lwd = 2, border = "red", col = "blue")
plot(province, lty = 2, lwd = 2, border = "red", col = "blue")
################################################################################
#
# Mapping variations (like mapping data)
#
################################################################################
#
# Colour province 1, 5, 10 red, rest blue
#
plot(province, lwd = 1, border = "gray50",
col = ifelse(province@data$ADM1_PCODE %in% c("01", "05", "10"), "red", "blue"))
#
# province pcode 08, 09, 20, 22 in green, rest blue
#
plot(province, lwd = 1, border = "gray50",
col = ifelse(province@data$ADM1_PCODE %in% c("08", "09", "20", "22"), "green", "blue"))
################################################################################
#
# Map make believe data on immunisation coverage
#
################################################################################
#
# create sample data for immunisation coverage per province
#
imm <- sample(0:100, 22, replace = TRUE)
#
# Add imm to province data
#
province@data$imm <- imm
#
# Create immunisation coverage classes
#
province@data$immclass <- base::cut(x = province@data$imm,
breaks = c(0, 20, 40, 60, 80, 100),
labels = FALSE)
#
# Specify mapping colour scheme
#
colourscheme <- c("#eff3ff", "#c6dbef", "#9ecae1",
"#6baed6", "#3182bd", "#08519c")
#
# Plot immunisation coverage results
#
plot(province, lwd = 1, border = "gray50",
col = ifelse(province@data$immclass == 0, colourscheme[1],
ifelse(province@data$immclass == 1, colourscheme[2],
ifelse(province@data$immclass == 2, colourscheme[3],
ifelse(province@data$immclass == 3, colourscheme[4],
ifelse(province@data$immclass == 4, colourscheme[5], colourscheme[6]))))))
#
# Plot immunisation coverage results - elegant and efficient solution
#
plot(province, lwd = 1, border = "gray50",
col = colourscheme[province@data$immclass + 1])
#
# Add province names
#
text(x = province, labels = "ADM1_EN", cex = 0.3)
################################################################################
#
# Map maternal mortality
#
################################################################################
#
# Read province data
#
pdata <- read.csv("provincedata.csv")
head(pdata)
pdata2015 <- pdata[pdata$year == 2015, ]
#
# Add deadhf + deadnothf for total deaths
#
pdata2015$totaldead <- pdata2015$deadhf + pdata2015$deadnothf
popdata <- sample(100000:300000, 22, replace = FALSE)
popdata
pop <- data.frame("pcode" = 1:22, "pop" = popdata)
pdata2015 <- merge(pdata2015, pop, by = "pcode")
################################################################################
#
#
#
################################################################################