Using Mapview

The data is stored in water_insecurity.rdata in this folder.

It contains the following data frames:

library(mapview)
library(leaflet)
library(plainview)
library(leafsync)
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
load("water_insecurity.rdata")
head(fulldata)
Simple feature collection with 6 features and 12 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -2348098 ymin: 1248263 xmax: -970234.9 ymax: 2234226
Projected CRS: NAD83 / Conus Albers
# A tibble: 6 × 13
  geoid county          state year                   geometry total_pop plumbing
  <chr> <chr>           <chr> <chr>        <MULTIPOLYGON [m]>     <dbl>    <dbl>
1 04001 Apache County   Ariz… 2022  (((-1229881 1641290, -12…     65432     2440
2 06037 Los Angeles Co… Cali… 2022  (((-2066923 1403703, -20…   9721138     6195
3 06097 Sonoma County   Cali… 2022  (((-2336933 2089875, -23…    482650      148
4 06001 Alameda County  Cali… 2022  (((-2268018 1957396, -22…   1628997      808
5 06045 Mendocino Coun… Cali… 2022  (((-2337142 2234226, -23…     89783       18
6 08077 Mesa County     Colo… 2022  (((-1123223 1807304, -11…    158636      128
# ℹ 6 more variables: percent_lacking_plumbing <dbl>, name <chr>, adi <dbl>,
#   financial_strength <dbl>, economic_hardship_and_inequality <dbl>,
#   Educational_Attainment <dbl>

We can use the mapview package to start exploring the dataset. We can view all rows of the data:

mapview(fulldata)

We can filter by state:

wa_counties <- fulldata |>
  dplyr::filter(state == "Washington") |>
  dplyr::filter(year == 2022)

We can visualize two variables as different layers by adding them together. Try clicking on the different layers (use the layer button in the top left).

m1 <- mapview(wa_counties, zcol=c("adi")) 
m2 <- mapview(wa_counties, zcol=c("percent_lacking_plumbing"))

m1 + m2

Or we can sync two maps:

sync(m1, m2)

We can also plot the various factors of ADIs for each county:

or_counties_2023 <- fulldata |>
  dplyr::filter(state == "Oregon") |>
  dplyr::filter(year == 2023)


or_counties_2022 <- fulldata |>
  dplyr::filter(state == "Oregon") |>
  dplyr::filter(year == 2022)

# Function to make mini data frames of each row for geom_bar()
select_adi_vars <- function(x){
    x |>
      select(geoid, adi, county, financial_strength, economic_hardship_and_inequality, Educational_Attainment) |>
      pivot_longer(cols=-c(geometry, geoid, county), names_to = "variable", values_to = "value") 
}

# apply function to each row of data frame
or_counties_adi <- or_counties_2022 |>
  rowwise() |>
  select_adi_vars()

## cycle over the geoids and make plots for each
or_plot_data <- map(unique(or_counties_adi$geoid), function(x){
  county_name <- or_counties_adi |>
    filter(geoid == x) |>
    select(county) |>
    slice(1)
    
  or_counties_adi |>
    filter(geoid == x) |>
    ggplot() +
    aes(x=variable, y=value, fill=variable)+
    geom_bar(stat="identity", show.legend=FALSE) +
    ggtitle(county_name) + 
    ylim(0, 150) +
    theme(axis.text.x = element_text(angle = 45)) +
    scale_x_discrete(labels=c("adi" = "ADI", "Educational_Attainment" = "Edu",
                              "economic_hardship_and_inequality" = "Hard", "financial_strength" = "Money" ))

})

mapview(or_counties_2022, zcol = "percent_lacking_plumbing", popup=leafpop::popupGraph(or_plot_data), legend=FALSE) 
Warning: Removed 1 row containing missing values or values outside the scale range
(`geom_bar()`).