GIS- Missing Countries when I split choropleths maps using facet_wrap()

,

Hello forum

I’m building a choropleths maps to present completeness of indicator of Africa countries , I want to facet it into multiples maps indicator column and fill by gradient column as the code show.
When I use face_wrap() function, the multiple maps appear with missing countries in the map that change the visual of the continent.

The dataset have geometry information, country, indicator, value, gradient.

I want to split by indicator column and value column is the gradient for the legend

Can any one help me so solve this problem ?

######################################################
# load packages
pacman::p_load(
  rio,        # importing data  
  here,       # relative file pathways  
  janitor,    # data cleaning and tables
  lubridate,  # working with dates
  matchmaker, # dictionary-based cleaning
  epikit,     # age_categories() function
  sf,            # to manage spatial data using a Simple Feature format
  tmap,          # to produce simple maps, works for both interactive and static maps
  janitor,       # to clean column names
  OpenStreetMap, # to add OSM basemap in ggplot map
  spdep,          # spatial statistics
  tidyverse   # data management and visualization
)


dataset_joao <- data.frame(
  stringsAsFactors = FALSE,
  country =c("Algeria","Algeria","Algeria","Algeria","Algeria","Angola","Angola","Angola","Angola","Angola"),
  indicator =c("Lymphatic filariasis","Onchocerciasis","Schistosomiasis", "Soil transmitted Helminthiasis","Trachoma", 
               "Lymphatic filariasis", "Onchocerciasis","Schistosomiasis","Soil transmitted Helminthiasis""Trachoma"),
  value = c("3", "3", "2", "2", "1","1", "1", "1","1","1"),
  gradient = c("Not endemic", "Pending","Endemic","Requiring PC","Not endemic", "Pending","Endemic","Requiring PC","Pending","Endemic", ),
  iso_3 = c("ALG","ALG","ALG","ALG","ALG","ANG","ANG","ANG",,"ANG","ANG")
)

ggplot(data = dataset_joao, aes(fill = gradient )) +  # Aesthetic for the fill color
  geom_sf(
    color = "black",            # Border color
    linewidth = 0.3,            # Border width
    alpha = 0.9,                # Transparency
    position = "identity"       # Default position adjustment
  ) +
  theme_void() +                # No axes, grid, etc.
  guides(
    fill = guide_legend(reverse = TRUE, title = NULL)  # Legend settings
  ) +
  scale_fill_manual(
    values = c("Endemic" = "#F42617",
               "Pending" = "#FCFF73",
               "Not Endemic" = "#6AFD20"
    ),
    name = "Reporting\nperformance (%)",
    na.value = "white"
  )+
  theme(
    panel.background = element_rect(fill = "#C3E7FE"),  # Background color
    axis.text.x = element_blank(),  # Remove x-axis text
    axis.text.y = element_blank(),  # Remove y-axis text
    strip.text = element_text(size = 10)  # Adjust facet label size
  ) +
  facet_wrap(
    facets = "indicator",        # Facet by 'indicator' variable
    scales = "fixed",            # Fixed scales across facets
    ncol = 2                    # Number of columns in facets
  )
#> Error in parse(text = input): <text>:23:107: unexpected string constant
#> 22:   indicator =c("Lymphatic filariasis","Onchocerciasis","Schistosomiasis", "Soil transmitted Helminthiasis","Trachoma", 
#> 23:                "Lymphatic filariasis", "Onchocerciasis","Schistosomiasis","Soil transmitted Helminthiasis""Trachoma"
#>     

Hi Joao!

The reprex doesn’t work unfortunately.

From the bottom right facet, I can see that some of the rows in your data are NA for the indicator column. You need to make sure that you have one row in your dataset for each combination of country and indicator (e.g. country == Morocco, indicator == HAT, gradient == NA).

You can use the complete() function from the tidyr package during your data cleaning, which will create a row for each combination of indicator and country, then replace the missing gradient values with something more meaningful.

# Create all combinations of country and indicator
expanded_data <- data %>%
  # Expand to have rows for all unique combinations of country and indicator
  complete(country, indicator) %>%
  # Fill in the gradient column with "Unknown" if missing
  mutate(gradient = if_else(is.na(gradient), "Unknown", gradient))
2 Likes