@@ -11,9 +11,7 @@ library(readxl)
11
11
library(readr )
12
12
library(writexl )
13
13
library(units )
14
- library(ggsn )
15
14
library(arrow )
16
- # library(geoarrow)
17
15
library(sfarrow )
18
16
library(osmdata )
19
17
library(viridis )
@@ -23,6 +21,9 @@ library(DescTools)
23
21
options(scipen = 9999 )
24
22
options(lifecycle_verbosity = " warning" )
25
23
24
+ library(ggsn )
25
+ # devtools::install_github('mansueto-institute/ggsn') # fork of 'oswaldosantos/ggsn'
26
+
26
27
# Load aggregation function
27
28
setwd(dirname(rstudioapi :: getActiveDocumentContext()$ path ))
28
29
getwd()
@@ -134,17 +135,6 @@ write_xlsx(list('dictionary_labels' = file_labels, 'dictionary_levels' = file_gr
134
135
135
136
# Aggregation arguments ---------------------------------------------------
136
137
137
- log_10 <- function (x ) {
138
- x = replace_na(na_if(na_if(na_if(log10(x ), NaN ), Inf ), - Inf ), 0 )
139
- x = ifelse(x < 1 , 1 , x )
140
- return (x )
141
- }
142
-
143
- share <- function (x ) {
144
- x = replace_na(na_if(na_if(na_if(x / sum(x ), NaN ), Inf ), - Inf ), 0 )
145
- return (x )
146
- }
147
-
148
138
sum_cols = c(" k_complexity_weighted_landscan_un" , " k_complexity_weighted_worldpop_un" ,
149
139
" block_count" , " block_area_m2" , " block_hectares" , " block_area_km2" , " block_perimeter_meters" , " building_area_m2" ,
150
140
" building_count" , " parcel_count" , " landscan_population" , " landscan_population_un" , " worldpop_population" , " worldpop_population_un" ,
@@ -1045,12 +1035,14 @@ if (!file.exists(paste0("data/africa_geodata.parquet"))) {
1045
1035
print(' africa_geodata.parquet already downloaded.' )
1046
1036
}
1047
1037
1048
- area_data <- arrow :: open_dataset(paste0(' data/africa_geodata.parquet' )) %> %
1038
+ # area_data <- arrow::open_dataset(paste0('data/africa_geodata.parquet')) %>%
1039
+ area_data <- arrow :: open_dataset(' data/lusaka_blocks.parquet' ) %> %
1049
1040
filter(urban_id %in% c(' ghsl_3798' ,' periurban_925' )) %> %
1050
1041
read_sf_dataset() %> %
1051
1042
st_set_crs(4326 ) %> %
1052
1043
st_make_valid()
1053
1044
1045
+
1054
1046
# Zoom map ----------------------------------------------------------------
1055
1047
1056
1048
sites <- data.frame (place = c(' nga' ,' sle' ,' zmb' ),
@@ -1109,8 +1101,8 @@ area_data_framed <- area_data %>%
1109
1101
theme_void() + inset_element(zoom_inset , left = - .5 , bottom = .12 , right = 1 , top = .55 , align_to = ' full' )
1110
1102
)
1111
1103
1112
- df_combined_prep %> % group_by(country_code , country_name ) %> %
1113
- tally() %> % print(n = 100 )
1104
+ # df_combined_prep %>% group_by(country_code, country_name) %>%
1105
+ # tally() %>% print(n = 100)
1114
1106
1115
1107
# buildings <- arrow::open_dataset('buildings_polygons_ZMB.parquet')) %>%
1116
1108
# filter(gadm_code %in% unique(area_data_framed$gadm_code))%>%
@@ -1162,7 +1154,7 @@ colorhexes <- colorRampPalette(c('#93328E','#CF3F80','#F7626B','#FF925A','#FFC55
1162
1154
fill = ' white' , alpha = 0 , linewidth = 1.7 ) +
1163
1155
geom_sf(data = sites %> % filter(place == place_var ), color = ' #434343' ,
1164
1156
fill = ' #434343' , alpha = 0 , linewidth = .5 ) +
1165
- scale_fill_manual(expand = c(0 ,0 ), values = c(grey2 , colorhexes ), name = ' Block complexity ' ) +
1157
+ scale_fill_manual(expand = c(0 ,0 ), values = c(grey2 , colorhexes ), name = ' Block\n complexity ' ) +
1166
1158
theme_void() + theme(text = element_text(color = " #333333" ),
1167
1159
legend.position = ' none' ,
1168
1160
legend.spacing.x = unit(1 , ' pt' ),
@@ -1176,10 +1168,10 @@ colorhexes <- colorRampPalette(c('#93328E','#CF3F80','#F7626B','#FF925A','#FFC55
1176
1168
legend.title = element_blank(),
1177
1169
plot.caption = element_text(size = 11 , hjust = .5 , vjust = 5 ), # margin=margin(0,0,0,0)),
1178
1170
axis.text = element_blank()) +
1179
- ggsn :: scalebar(y.min = st_bbox(area_data_framed )$ ymin - (height_decdegs * .03 ),
1180
- x.min = st_bbox(area_data_framed )$ xmin ,
1181
- y.max = st_bbox(area_data_framed )$ ymax ,
1182
- x.max = st_bbox(area_data_framed )$ xmax ,
1171
+ ggsn :: scalebar(y.min = st_bbox(area_data_framed )$ ymin - (height_decdegs * .03 ),
1172
+ x.min = st_bbox(area_data_framed )$ xmin ,
1173
+ y.max = st_bbox(area_data_framed )$ ymax ,
1174
+ x.max = st_bbox(area_data_framed )$ xmax ,
1183
1175
location = ' bottomleft' ,
1184
1176
height = .01 , box.fill = c(' #333333' ,' #ffffff' ),
1185
1177
border.size = .4 , st.color = ' #333333' , st.size = 2.5 , box.color = ' #333333' ,
@@ -1193,7 +1185,7 @@ colorhexes <- colorRampPalette(c('#93328E','#CF3F80','#F7626B','#FF925A','#FFC55
1193
1185
geom_sf(data = streets , color = ' white' , fill = ' white' , linewidth = 1 ) +
1194
1186
geom_sf(data = sites %> % filter(place == place_var ), color = ' white' , fill = ' white' , alpha = 0 , linewidth = 1.5 ) +
1195
1187
scale_fill_manual(expand = c(0 ,0 ), breaks = c(' 1' , ' 2' , ' 3' , ' 4' , ' 5' , ' 6' , ' 7' , ' 8' , ' 9' , ' 10+' , ' Off\n network' ),
1196
- values = c(grey2 , colorhexes ), name = ' Block complexity ' ) +
1188
+ values = c(grey2 , colorhexes ), name = ' Block\n complexity ' ) +
1197
1189
theme_void() +
1198
1190
theme(legend.position = ' none' ))
1199
1191
@@ -1216,7 +1208,7 @@ colorhexes <- colorRampPalette(c('#93328E','#CF3F80','#F7626B','#FF925A','#FFC55
1216
1208
geom_sf(data = layers %> % filter(block_property == ' on-network-streets' ,
1217
1209
block_id %in% c(" ZMB.5.6_2_9371" )),
1218
1210
fill = ' yellow' , color = ' white' , alpha = .8 , linewidth = .7 , lineend = ' round' ) + # linetype = "dashed",
1219
- scale_fill_manual(name = ' Block complexity ' , values = c(grey2 , colorhexes ), breaks = c(' 1' , ' 2' , ' 3' , ' 4' , ' 5' , ' 6' , ' 7' , ' 8' , ' 9' , ' 10+' , ' Off\n network' )) +
1211
+ scale_fill_manual(name = ' Block\n complexity ' , values = c(grey2 , colorhexes ), breaks = c(' 1' , ' 2' , ' 3' , ' 4' , ' 5' , ' 6' , ' 7' , ' 8' , ' 9' , ' 10+' , ' Off\n network' )) +
1220
1212
labs(subtitle = ' ' ) +
1221
1213
theme_void() + theme(legend.title = element_text(size = 14 , face = ' bold' ),
1222
1214
legend.text = element_text(size = 14 ),
@@ -1237,7 +1229,7 @@ colorhexes <- colorRampPalette(c('#93328E','#CF3F80','#F7626B','#FF925A','#FFC55
1237
1229
geom_sf(data = layers %> % filter(block_property == ' on-network-streets' ,
1238
1230
block_id %in% c(" ZMB.5.6_2_9339" , " ZMB.5.6_2_9334" , " ZMB.5.6_2_9335" )),
1239
1231
fill = ' yellow' , color = ' white' , alpha = .8 , linewidth = .7 , lineend = ' round' ) + # linetype = "dashed",
1240
- scale_fill_manual(name = ' Block complexity ' , values = c(grey2 , colorhexes ), breaks = c(' 1' , ' 2' , ' 3' , ' 4' , ' 5' , ' 6' , ' 7' , ' 8' , ' 9' , ' 10+' , ' Off\n network' )) +
1232
+ scale_fill_manual(name = ' Block\n complexity ' , values = c(grey2 , colorhexes ), breaks = c(' 1' , ' 2' , ' 3' , ' 4' , ' 5' , ' 6' , ' 7' , ' 8' , ' 9' , ' 10+' , ' Off\n network' )) +
1241
1233
labs(subtitle = ' ' ) +
1242
1234
theme_void() + theme(legend.position = ' none' ,
1243
1235
plot.subtitle = element_text(size = 10 ),
@@ -1246,11 +1238,16 @@ colorhexes <- colorRampPalette(c('#93328E','#CF3F80','#F7626B','#FF925A','#FFC55
1246
1238
design <- "
1247
1239
AAAABBBBBB
1248
1240
AAAABBBBBB
1249
- CCCCDDDDEE
1250
- CCCCDDDDEE
1241
+ AAAABBBBBB
1242
+ AAAABBBBBB
1243
+ AAAABBBBBB
1244
+ CCCDDDEEFF
1245
+ CCCDDDEEFF
1246
+ CCCDDDEEFF
1247
+ CCCDDDEEFF
1251
1248
"
1252
1249
1253
- (layers_viz <- africa_zoom + plot_k_discrete + map_block + l1 + l2 +
1250
+ (layers_viz <- africa_zoom + plot_k_discrete + l1 + map_block + l2 + guide_area() +
1254
1251
# plot_layout(design = design, guides = "collect", tag_level = 'new') + plot_annotation( tag_levels = c('A')) &
1255
1252
plot_layout(design = design , guides = " collect" ) + plot_annotation(tag_levels = list (c(" A" , " B" , " C" , " D" , " E" , " F" ))) &
1256
1253
theme(plot.tag = element_text(size = 12 )))
@@ -1393,7 +1390,7 @@ colorhexes <- colorRampPalette(c('#93328E','#CF3F80','#F7626B','#FF925A','#FFC55
1393
1390
1394
1391
(plot_k_discrete <- ggplot() +
1395
1392
geom_sf(data = area_data , aes(fill = as.factor(k_labels )), color = ' #ffffff' , linewidth = .0075 ) +
1396
- scale_fill_manual(expand = c(0 ,0 ), values = c(grey2 , colorhexes ), name = ' Block complexity ' ) +
1393
+ scale_fill_manual(expand = c(0 ,0 ), values = c(grey2 , colorhexes ), name = ' Block\n complexity ' ) +
1397
1394
labs(caption = paste0(' Population-weighted average block complexity: ' , area_data %> % st_drop_geometry() %> %
1398
1395
summarise(wm_var = weighted.mean(as.integer(k_complexity ), landscan_population_un )) %> % pull() %> % round(. ,2 ))) +
1399
1396
# guides(color = guide_legend(nrow = 1, label.position = "bottom", keywidth = 2, keyheight = 1),
0 commit comments