Choropleth map - regions must be highlighted

Hi, I’m working on a choropleth map of cases in two regions of the PH, province-level data is not necessary, but the map that appeared pinpointed the cases only to a small portion of the region and not to the entire region. The two regions (where the cases came from) must be highlighted.

ph_base_layer_nat <- 
  read_sf(here("shapefiles", "Regions.shp")) %>%  #shapefile of regions I downloaded from github(Philippines-psgc shapefiles -regions)
  clean_names()

national_map <- ggplot() +

  geom_sf(data = ph_base_layer_nat, 
          fill = "plum",
          colour = "black")


#Dataframe I created to be added in the map (dengue cases in two regions in PH)
cases_data <- data.frame(
  Region = c("Region I", "Region II"),
  Latitude = c(16.083214, 18.248962),
  Longitude = c(120.619989, 121.878784),
  Cases = c(6972, 5385)
)

saveRDS(cases_data, "cases_data.rds")

outbreak_linelist <- 
  import(here("cases_data.rds")) 

outbreak_linelist_sf <- 
  outbreak_linelist %>% 
  st_as_sf(coords = c("Longitude", "Latitude"), crs = 4326)

(outbreak_linelist_sf) %>% select(1:3, geometry)


#Choropleth map
outbreak_map <- 
  ggplot() +
 geom_sf(data = ph_base_layer_nat, 
          fill = NA, 
          colour = "black") +
  geom_sf(data = outbreak_linelist_sf,
          aes(color = Cases), size = 1)
outbreak_map  

I hope you could help/guide me.
Thank you in advance.

Regards,
Echo

It seems you are trying to create a choropleth map to visualize dengue cases in two regions of the Philippines. Your code currently plots points for the cases, but you want to highlight the entire regions instead. Is that right?

If so, you need to merge your cases data with the shapefile data so that the regions are colored based on the cases. Here’s what I’d do:

  • Remove lat and long data: In your code, you had latitude and longitude data for specific points. Since you’re now highlighting entire regions, this point-specific data is no longer necessary.
  • Merge your data with the shapefile: The major change I’d do is merge your cases_data with the ph_base_layer_nat shapefile data. You can use the left_join function from the dplyr package. This will allow you to color the regions based on the number of cases. Make sure the column names used in the by argument correctly match the region names in both datasets.
  • Create choropleth map: Instead of using geom_sf to plot points, you can use geom_sf with the fill aesthetic set to the Cases column. This fills each region with a color intensity based on the number of cases.
  • Remove point-specific plotting: Your code included lines for plotting specific points (latitude and longitude). I’d remove them as the focus is now on highlighting entire regions.

My draft thinking:

# Read shapefile for Philippines regions
ph_base_layer_nat <- read_sf("path_to/Regions.shp") %>%  # Replace with your actual path
                     clean_names()

# Dataframe with dengue cases in two regions in PH
cases_data <- data.frame(
  Region = c("Region I", "Region II"),
  Cases = c(6972, 5385)
)

# Merge the cases data with the shapefile data
ph_base_layer_nat <- ph_base_layer_nat %>% 
                     left_join(cases_data, by = c("name" = "Region"))

# Create choropleth map
outbreak_map <- ggplot(ph_base_layer_nat) +
                geom_sf(aes(fill = Cases), color = "black") +
                scale_fill_viridis_c() +  
                theme_minimal() +
                labs(fill = "Dengue Cases")

# Display the map
print(outbreak_map)

Highly appreciated, it worked.
I also noticed that some of the names in the shapefile of Regions I downloaded were anymore updated, hence I made some updates through mutate.
Thank you very much.

Regards,
Echo

1 Like

Yay that’s awesome!

1 Like