-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.R
79 lines (71 loc) · 3.64 KB
/
helpers.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
# Note: percent map is designed to work with the afghanistan admin data set
# It may not work correctly with other data sets if their row order does
# not exactly match the order in which the maps package plots districts and provinces
province_map <- function(all, province, fill_prov, fill, size = 3, country = FALSE) {
if (country) {
p <- ggplot(all, aes(x = long, y = lat, group = group)) + geom_polygon(fill="white", color = "black") +
geom_polygon(data=province, aes(x=long, y=lat, group=group), fill=fill_prov, color = "black") +
geom_path(data = province, aes(x = long, y = lat), color = "black")+ coord_fixed() +
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(province, aes(x = long, y = lat, group = group)) + geom_polygon(fill="white") +
geom_path(data = province, aes(x = long, y = lat), color = "black")+ coord_fixed() +
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())
}
print(p)
}
district_map <- function(all, province, districts, centroids, fill_prov, fill, size = 3, country = FALSE) {
if (country) {
p <- ggplot(all, aes(x = long, y = lat, group = group)) + geom_polygon(fill="white", color = "black") +
geom_polygon(data=province, aes(x=long, y=lat, group=group), fill=fill_prov, color = "black") +
geom_path(data = province, aes(x = long, y = lat), color = "black")+ coord_fixed() +
lapply(districts,geom_polygon, mapping=aes(x=long, y=lat), fill= fill) +
lapply(districts,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(province, aes(x = long, y = lat, group = group)) + geom_polygon(fill="white") +
geom_path(data = province, aes(x = long, y = lat), color = "black")+ coord_fixed() +
lapply(districts,geom_polygon, mapping=aes(x=long, y=lat), fill= fill) +
lapply(districts,geom_path, mapping=aes(x=long, y=lat))+
geom_text(data = centroids, aes(label = DIST_NA_EN, x = long, y = lat, group = DIST_NA_EN), size = size) +
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())
}
print(p)
}
getdf2 <- function(country.spdf, province) {
sub.shape <- country.spdf[country.spdf$PROV_NA_EN == province,]
country.df <- fortify(sub.shape, region = "DIST_NA_EN")
}
getDistricts <- function(country.df) {
district_list <- unique(country.df$id)
}
getCentroids <- function(centroids, province, districts){
cent2 <-subset(centroids, (DIST_NA_EN %in% districts))
cent2 <-subset(cent2, PROV_NA_EN == province)
}
getPlot1 <- function(df, province){
toplot1 = subset(df, id == province)
}
getPlot2 <- function(df, districts){
toplot2 <- replicate(length(districts), data.frame())
for (i in 1:length(districts)) {
toplot2[[i]] <- subset(df, id == districts[i])
}
return(toplot2)
}