-
Notifications
You must be signed in to change notification settings - Fork 0
/
District_plot.R
177 lines (122 loc) · 5.36 KB
/
District_plot.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#Create a map of the administrative areas of Afghanistan
#========================================================
#Original methods explained in stackoverflow in following link:
#http://stackoverflow.com/questions/17723822/administrative-regions-map-of-a-country-with-ggmap-and-ggplot2
#http://stackoverflow.com/questions/19718814/how-to-draw-ggmap-with-two-different-administrative-boundaries
#Data accessed on http://www.gadm.org/ on 9th October 2014
#Load the libraries needed
library(ggplot2)
library(rgdal)
library(RCurl)
#Download the data if it doesn't exist.
read.url <- function(url, ...){
dataFile <- tempfile()
download.file(url, destfile = dataFile, method = "curl")
return(dataFile)
}
if (file.exists("AFG_adm2.RData")) {
print("Loading existing data from file")
load("AFG_adm2.RData")
} else {
print("Downloading Data from Web URL:")
URL <- "http://data.biogeo.ucdavis.edu/data/gadm2/R/AFG_adm2.RData"
print(URL)
tempfile = read.url(URL)
save(tempfile,file="AFG_adm2.RData")
load(tempfile)
}
afghanistan.adm2.spdf <- get("gadm")
#print(afghanistan.adm2.spdf)
country = FALSE
#Extracting names and centoids of administrative areas for plotting
# Get centroids of spatialPolygonDataFrame and convert to dataframe
# for use in plotting area names.
afghanistan.adm2.centroids.df <- data.frame(long = coordinates(afghanistan.adm2.spdf)[, 1],
lat = coordinates(afghanistan.adm2.spdf)[, 2])
# Get names and id numbers corresponding to administrative areas
afghanistan.adm2.centroids.df[, 'ID_2'] <- afghanistan.adm2.spdf@data[,'ID_2']
afghanistan.adm2.centroids.df[, 'NAME_2'] <- afghanistan.adm2.spdf@data[,'NAME_2']
afghanistan.adm2.centroids.df[, 'NAME_1'] <- afghanistan.adm2.spdf@data[,'NAME_1']
#Create ggplot with labels for administrative areas
trim <- function (x) gsub("^\\s+|\\s+$", "", x)
UIinput_province <- function(){
#Ask for user input
x <- readline(prompt = "Please enter the name of the province you would like to plot: ")
#Can it be converted?
x <- toString(x)
return(x)
}
UIinput_districts <- function(){
#Ask for user input
x <- readline(prompt = "Please enter the name of the districts you would like to plot, separated by commas: ")
x <- unlist(strsplit(x, ","))
x <- unlist(lapply(x, trim))
return(x)
}
UIinput_colour <- function(){
#Ask for user input
x <- readline(prompt = "Please specify the colour you would like to use, if you are feeling lucky. Red is the default option: ")
#Can it be converted?
x <- toString(tolower(x))
if(x == ""){
x <- "red"
}
return(x)
}
UIinput_country<- function(){
#Ask for user input
x <- readline(prompt = "Would you like to print a country-wide map [y/n]?: ")
#Can it be converted?
x <- toString(tolower(x))
if(x == "y"){
country = TRUE
}
return(country)
}
province <- UIinput_province()
#Subset just the province required for level 2 data
sub.shape <- afghanistan.adm2.spdf[afghanistan.adm2.spdf$NAME_1 == province,]
afghanistan.adm1.df <- fortify(afghanistan.adm2.spdf, region = "NAME_1")
afghanistan.adm2.df <- fortify(sub.shape, region = "NAME_2")
toplot0 <- afghanistan.adm1.df <- fortify(afghanistan.adm2.spdf, region = "NAME_0")
print(paste("You have selected the", province, "province."))
print(paste("Districts in", province, ":"))
print(levels(as.factor(afghanistan.adm2.df$id)))
districts <- UIinput_districts()
if (districts == "ALL") {districts <- levels(as.factor(afghanistan.adm2.df$id))}
UIcolour <- UIinput_colour()
country <- UIinput_country()
toplot1 = subset(afghanistan.adm1.df, id == province)
toplot2 <- replicate(length(districts), data.frame())
for (i in 1:length(districts)) {
toplot2[[i]] <- subset(afghanistan.adm2.df, id == districts[i])
}
#district_cent <- unlist(lapply(districts,FUN=gsub, pattern = "\\.[0-9]",replacement = ""))
cent2 <-subset(afghanistan.adm2.centroids.df, (NAME_2 %in% districts))
cent2 <-subset(cent2, NAME_1 == province)
if (country) {
p <- ggplot(toplot1, aes(x = long, y = lat, group = group)) + geom_polygon(fill="white") +
geom_polygon(data=toplot0, aes(x=long, y=lat, group=group), fill="white", color = "black") +
geom_path(data = toplot1, aes(x = long, y = lat), color = "black")+
lapply(toplot2,geom_polygon, mapping=aes(x=long, y=lat), fill= UIcolour) +
lapply(toplot2,geom_path, mapping=aes(x=long, y=lat))+
labs(x=" ", y=" ") +
theme_bw() +
theme(panel.grid.minor=element_blank(), panel.grid.major=element_blank()) +
theme(axis.ticks = element_blank(), axis.text.x = element_blank(), axis.text.y = element_blank()) +
theme(panel.border = element_blank())
} else {
p <- ggplot(toplot1, aes(x = long, y = lat, group = group)) + geom_polygon(fill="white") +
geom_path(data = toplot1, aes(x = long, y = lat), color = "black")+
lapply(toplot2,geom_polygon, mapping=aes(x=long, y=lat), fill= UIcolour) +
lapply(toplot2,geom_path, mapping=aes(x=long, y=lat))+
geom_text(data = cent2, aes(label = NAME_2, x = long, y = lat, group = NAME_2), size = 5) +
labs(x=" ", y=" ") +
theme_bw() +
theme(panel.grid.minor=element_blank(), panel.grid.major=element_blank()) +
theme(axis.ticks = element_blank(), axis.text.x = element_blank(), axis.text.y = element_blank()) +
theme(panel.border = element_blank())
}
#Plot and save the result
print(p)
ggsave(paste("Afghanistan",province,"all3.png",sep="_"), width=4, height=4, dpi=300)