Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
haachicanoy committed Mar 28, 2022
1 parent 160af69 commit 3dafbc7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
4 changes: 2 additions & 2 deletions hotspots/02_hotspots_identification_category.R
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ smm_df <- ip_id %>%
htp_ct <- list.files(path = paste0(root,'/data/',iso,'/_results/hotspots'), pattern = paste0(iso,'_',ip,'_*[a-z]*_hotspots.tif$'), full.names = T)
hotspots <- terra::rast(htp_ct)
# Remove natural resources category from the identification of the hotspots
cond <- grep(pattern = 'rsrs', x = terra::sources(hotspots))
if(length(cond) > 0){hotspots <- hotspots[[base::setdiff(1:terra::nlyr(hotspots),cond)]]}
# cond <- grep(pattern = 'rsrs', x = terra::sources(hotspots))
# if(length(cond) > 0){hotspots <- hotspots[[base::setdiff(1:terra::nlyr(hotspots),cond)]]}
# Compute general hotspots
hotspots <- hotspots %>% sum(na.rm = T)
hotspots[hotspots == 0] <- NA
Expand Down
14 changes: 13 additions & 1 deletion hotspots/02_labeling_climate_clusters.R
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,21 @@ rawv_medn_prec %>%
ggplot2::ggplot(aes(x = factor(clust), y = trnd_cwdf)) +
ggplot2::geom_boxplot()
strat_sample %>%
ggplot2::ggplot(aes(x = factor(clust), y = trnd_cwdf)) +
ggplot2::ggplot(aes(x = reorder(clust, trnd_prec, FUN = median), y = trnd_prec)) +
ggplot2::geom_boxplot()

names(strat_sample)[-1] %>%
purrr::map(.f = function(vrbl){
eval(parse(text=paste0('lvl <- levels(reorder(strat_sample$clust, strat_sample$',vrbl,', FUN = median))')))
lvl <- paste(lvl, collapse = '-')
lvl <- data.frame(levels = lvl)
return(lvl)
}) %>%
dplyr::bind_rows() %>%
dplyr::pull('levels') %>%
table() %>%
sort(decreasing = T)

agricolae::kruskal(y = rawv_medn_prec$trnd_cwdf,
trt = factor(rawv_medn_prec$clust),
alpha = 0.05,
Expand Down
23 changes: 15 additions & 8 deletions hotspots/03_get_summary_tables_final_maps.R
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ fix_label <- function( rast_labs, labs = av_labs){

}

baseDir <- "//alliancedfs.alliance.cgiar.org/WS18_Afrca_K_N_ACO/1.Data/Palmira/CSO/data/" # baseDir <- 'D:/OneDrive - CGIAR/African_Crisis_Observatory/data/'
baseDir <- "//alliancedfs.alliance.cgiar.org/WS18_Afrca_K_N_ACO/1.Data/Palmira/CSO/data/"
w_mask <- raster::raster(paste0(baseDir, "_global/masks/mask_world_1km.tif"))

iso <- "KEN"
Expand Down Expand Up @@ -417,24 +417,31 @@ for(i in get_ip_names){

raster::writeRaster(ht_rast, paste0(to_share_dir, "/", ip_x, "_hotspots_map.tif"), overwrite = T)

rast_labs <- tibble( ID = unique(ht_rast[]) ) %>%
tidyr::drop_na() %>%
dplyr::left_join(., final_label_tbl, by = c("ID" = "rast_values")) %>%
dplyr::mutate(final_label = fix_label(rast_labs = ., labs = final_label_tbl %>%
rast_labs <- tibble( ID = unique(ht_rast[]) ) %>%
tidyr::drop_na() %>%
dplyr::left_join(., final_label_tbl, by = c("ID" = "rast_values")) %>%
dplyr::mutate(final_label = fix_label(rast_labs = ., labs = final_label_tbl %>%
dplyr::filter(!is.na(category)) ),
chars = nchar(final_short_lab),
seq = 1:nrow(.)) %>%
arrange(chars) %>%
seq = 1:nrow(.)) %>%
arrange(chars) %>%
dplyr::select(ID, final_label, seq)

rast_labs %>%
rast_labs %>%
dplyr::select(value_ID = ID, label = final_label) %>%
write_csv(., paste0(to_share_dir, "/", ip_x, "_hotspots_labels.csv"))

ht_rast_f <- raster::subs(ht_rast, rast_labs[, c("ID", "seq")])
ht_rast_f <- as.factor(ht_rast_f)
levels(ht_rast_f) <- data.frame(id = levels(ht_rast_f)[[1]], x = rast_labs$final_label )

tb <- read.csv(file = paste0(baseDir,iso,'/_results/hotspots/soc_eco_all_variables.csv')) # soc_eco_all_variables.csv # soc_eco_selected_variables.csv
tb$Code <- gsub(pattern = '{iso}', replacement = iso, x = tb$Code, fixed = T)
tb <- tb %>% dplyr::filter(IP_id == ip_x)
rg <- stringr::str_split(unique(tb$Region_value), ";") %>% unlist() %>% stringr::str_trim()
var_name <- unique(tb$Region_key)
shp_c <- shp_c[shp_c@data %>% dplyr::pull(!!var_name) %in% rg,]; rm(tb,rg,var_name)

hots_map <- tmap::tm_shape(shp_c)+
tm_borders(col = "gray50")+
tm_shape(ht_rast_f)+
Expand Down

0 comments on commit 3dafbc7

Please sign in to comment.