Urban Ipixuna: Spatial analysis of items
Load required libraries and data
rm(list = ls())
library(day2day)
library(ggplot2)
library(sf)
#> Linking to GEOS 3.10.2, GDAL 3.4.1, PROJ 8.2.1; sf_use_s2() is TRUE
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
path_proj <- git_path()
path_data <- file.path(path_proj, "data")
path_processed <- file.path(path_data, "processed")
list.files(file.path(path_proj, "src"), full.names = TRUE) |>
purrr::walk(source)
fidata <- st_read(file.path(path_processed, "fi-items-ipixuna-urban.gpkg"), as_tibble = TRUE)
#> Reading layer `fi-items-ipixuna-urban' from data source
#> `/home/rstudio/documents/projects/food-insecurity-mapping/data/processed/fi-items-ipixuna-urban.gpkg'
#> using driver `GPKG'
#> Simple feature collection with 200 features and 36 fields
#> Geometry type: POINT
#> Dimension: XY
#> Bounding box: xmin: -71.70038 ymin: -7.06058 xmax: -71.68109 ymax: -7.03724
#> Geodetic CRS: WGS 84
Prepare data by season
fidata <- fidata |>
st_jitter(factor = 0.02) %>%
bind_rows(., .) |>
within(season[(nrow(fidata)+1):(2*nrow(fidata))] <- "all") |>
mutate(season = factor(season, levels = c("all", "dry", "wet"),
labels = c("All", "Dry", "Wet")))
Spatial Distribution of food insecurity proportion
The locations of households were jittered, so it might not be that clear to see the spatial structure of food insecurity proportion.
ggplot(fidata) +
geom_sf(aes(col = fi_proportion)) +
facet_wrap(~ season) +
ggtitle("Jittered locations of households (Urban Ipixuna).") +
scale_color_distiller(palette = "RdBu") +
theme(panel.background = element_rect(fill = 'gray10'),
panel.grid.major = element_line(color = 'gray30'),
panel.grid.minor = element_line(color = 'green', size = 2))
#> Warning: The `size` argument of `element_line()` is deprecated as of ggplot2 3.4.0.
#> ℹ Please use the `linewidth` argument instead.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated.
Spatial Distribution of items
It is more clear to identify spatial pattern when analysing each item.
items_sf <- fidata |>
st_jitter(factor = 0.02) |>
dplyr::select(matches("^item"), registro, season) |>
tidyr::pivot_longer(
matches("^item"),
names_to = "item_name",
names_transform = list(item_name = function(x) factor(x, x)),
values_to = "item_value",
values_transform = list(item_value = function(x) factor(x, levels = c(1, 0)))
)
plotitems <- function(x) {
ggplot(x) +
geom_sf(aes(geometry = geom, col = item_value), size = 0.8) +
facet_grid(item_name ~ season) +
labs(color = "value", title = "Spatial distribution of items") +
theme(legend.position = "top")
}
Items of category A
items_sf |>
filter(item_name %in% grep("_A", levels(items_sf$item_name), value = TRUE)) |>
mutate(item_name = clean_item_names(item_name)) |>
plotitems()
Items of category B
items_sf |>
filter(item_name %in% grep("_B", levels(items_sf$item_name), value = TRUE)) |>
plotitems()
Items of category C
items_sf |>
filter(item_name %in% grep("_C", levels(items_sf$item_name), value = TRUE)) |>
plotitems()
Items of category D
items_sf |>
filter(item_name %in% grep("_D", levels(items_sf$item_name), value = TRUE)) |>
plotitems()
Time to execute the task
Only useful when executed with Rscript
.
proc.time()
#> user system elapsed
#> 5.358 0.376 5.420