Skip to contents
library(presentresults)
library(dplyr)
results_table <- expand.grid(analysis_var = c("msni_in_need", "comp_health_in_need", "comp_prot_in_need", "comp_edu_in_need", "comp_foodsec_in_need","comp_wash_in_need",        "comp_snfi_in_need"),
                             analysis_var_value = c(1,0),
                             group_var = "admin1",
                             group_var_value = c("PCODE1", "PCODE2"))

set.seed(12344)
results_table$stat <-runif(nrow(results_table))

This vignette will show an example how to create a table for the MSNA Indicator Maps 1.2 tool.

create_table_for_map

The results table (randomly generated) shown below present the results in a long format for 7 indicators and at the admin 1 :

  • msni_in_need: Multi Sector Need Index score
  • comp_health_in_need: Health sectoral composite score
  • comp_prot_in_need: Protection sectoral composite score
  • comp_edu_in_need: Education sectoral composite score
  • comp_foodsec_in_need: Food Security sectoral composite score
  • comp_wash_in_need: Water, Sanitation and Hygiene sectoral composite score
  • comp_snfi_in_need: Shelter and Non Food Items sectoral composite score
  • admin1: PCODE1 and PCODE2.

stat column represent the percentages.

head(results_table)
#>           analysis_var analysis_var_value group_var group_var_value       stat
#> 1         msni_in_need                  1    admin1          PCODE1 0.06471086
#> 2  comp_health_in_need                  1    admin1          PCODE1 0.93059035
#> 3    comp_prot_in_need                  1    admin1          PCODE1 0.05543592
#> 4     comp_edu_in_need                  1    admin1          PCODE1 0.06287199
#> 5 comp_foodsec_in_need                  1    admin1          PCODE1 0.18820633
#> 6    comp_wash_in_need                  1    admin1          PCODE1 0.74359703

Maps can only show one information per admin level. First thing is to filter the results table for the information to get the information to be map. In the example, the analysis variable value can be 1 or 0, where 1 means “in need”, and 0 means “not in need”. The map will show the percentages of household in needs, the results will be filtered for 1. Then the function create_table_for_map can be used.

results_table_filtered <- results_table |> 
  filter(analysis_var_value == "1")

results_table_recoded_5_classes <- results_table_filtered %>% 
  create_table_for_map(number_classes = 5)

results_table_recoded_5_classes
#> # A tibble: 2 × 8
#>   group_var_value msni_in_need comp_health_in_need comp_prot_in_need
#>   <fct>           <chr>        <chr>               <chr>            
#> 1 PCODE1          2            5                   2                
#> 2 PCODE2          3            2                   3                
#> # ℹ 4 more variables: comp_edu_in_need <chr>, comp_foodsec_in_need <chr>,
#> #   comp_wash_in_need <chr>, comp_snfi_in_need <chr>

If you want to use 6 classes, set the argument number_classes to 6.

results_table_filtered %>% 
  create_table_for_map(number_classes = 6)
#> # A tibble: 2 × 8
#>   group_var_value msni_in_need comp_health_in_need comp_prot_in_need
#>   <fct>           <chr>        <chr>               <chr>            
#> 1 PCODE1          2            6                   2                
#> 2 PCODE2          3            2                   3                
#> # ℹ 4 more variables: comp_edu_in_need <chr>, comp_foodsec_in_need <chr>,
#> #   comp_wash_in_need <chr>, comp_snfi_in_need <chr>

There can be 5 or 6 classes as follow:

5 classes

Class Value
1 0
2 <= 25%
3 <= 50%
4 <= 75%
5 <= 100%
empty Anything else

6 classes

Class Value
1 0
2 <= 20%
3 <= 40%
4 <= 60%
5 <= 80%
6 <= 100%
empty Anything else

Troubleshooting

If you have more than one value per indicator and admin level, you should get a warning from tidyr. You can use it to explore where the problem lies.

results_table %>% 
  create_table_for_map()
#> Warning: Values from `stat_recoded` are not uniquely identified; output will contain
#> list-cols.
#>  Use `values_fn = list` to suppress this warning.
#>  Use `values_fn = {summary_fun}` to summarise duplicates.
#>  Use the following dplyr code to identify duplicates.
#>   {data} |>
#>   dplyr::summarise(n = dplyr::n(), .by = c(group_var_value, analysis_var)) |>
#>   dplyr::filter(n > 1L)
#> # A tibble: 2 × 8
#>   group_var_value msni_in_need comp_health_in_need comp_prot_in_need
#>   <fct>           <list>       <list>              <list>           
#> 1 PCODE1          <chr [2]>    <chr [2]>           <chr [2]>        
#> 2 PCODE2          <chr [2]>    <chr [2]>           <chr [2]>        
#> # ℹ 4 more variables: comp_edu_in_need <list>, comp_foodsec_in_need <list>,
#> #   comp_wash_in_need <list>, comp_snfi_in_need <list>
results_table |>
  dplyr::summarise(n = dplyr::n(), .by = c(group_var_value, analysis_var)) |>
  dplyr::filter(n > 1L)
#>    group_var_value         analysis_var n
#> 1           PCODE1         msni_in_need 2
#> 2           PCODE1  comp_health_in_need 2
#> 3           PCODE1    comp_prot_in_need 2
#> 4           PCODE1     comp_edu_in_need 2
#> 5           PCODE1 comp_foodsec_in_need 2
#> 6           PCODE1    comp_wash_in_need 2
#> 7           PCODE1    comp_snfi_in_need 2
#> 8           PCODE2         msni_in_need 2
#> 9           PCODE2  comp_health_in_need 2
#> 10          PCODE2    comp_prot_in_need 2
#> 11          PCODE2     comp_edu_in_need 2
#> 12          PCODE2 comp_foodsec_in_need 2
#> 13          PCODE2    comp_wash_in_need 2
#> 14          PCODE2    comp_snfi_in_need 2

In this case there are 2 values for each combination of group_var_value and analysis_var.

results_table |>
  filter(group_var_value == "PCODE1" & analysis_var == "msni_in_need")
#>   analysis_var analysis_var_value group_var group_var_value       stat
#> 1 msni_in_need                  1    admin1          PCODE1 0.06471086
#> 2 msni_in_need                  0    admin1          PCODE1 0.64111149

There are two values, maps can only show one. The results table should be filtered to one value only.