rtmap

tmap different border colours for regions


I am producing a map of BC with 5 labeled regions based on border colour. Right now my approach is to split the original file into individual files, convert to MULTILINESTRING and use tm_lines to get the different colours.

Is this the only way possible or is there something faster/more efficient?

Not very familiar with tmap atm.

Current approach

library(bcmaps)
library(dplyr)
library(sf)
library(tmap)

ha_outline <- ha_outline <- bcmaps::health_ha() |> 
  rename_with(tolower, everything()) |> 
  mutate(colour = c("#3891A7", "#C3860D", "#C42E2E", "#67A63C", "#914FAB"))

interior_lines <- ha_outline |> 
  filter(hlth_authority_name == "Interior") |> 
  sf::st_cast("MULTILINESTRING")
fraser_lines <- ha_outline |> 
  filter(hlth_authority_name == "Fraser") |> 
  sf::st_cast("MULTILINESTRING")
vancoastal_lines <- ha_outline |> 
  filter(hlth_authority_name == "Vancouver Coastal") |> 
  sf::st_cast("MULTILINESTRING")
vanisland_lines <- ha_outline |> 
  filter(hlth_authority_name == "Vancouver Island") |> 
  sf::st_cast("MULTILINESTRING")
northern_lines <- ha_outline |> 
  filter(hlth_authority_name == "Northern") |> 
  sf::st_cast("MULTILINESTRING")

tm_shape(ha_outline) +
  tm_polygons() +
  tm_shape(interior_lines) +
  tm_lines(col="colour", lwd=2) +
  tm_shape(fraser_lines) +
  tm_lines(col="colour", lwd=2) +
  tm_shape(vancoastal_lines) +
  tm_lines(col="colour", lwd=2) +
  tm_shape(vanisland_lines) +
  tm_lines(col="colour", lwd=2) +
  tm_shape(northern_lines) +
  tm_lines(col="colour", lwd=2)

enter image description here

Thanks in advance!


Solution

  • Can be easily accomplished by using "hlth_authority_name" as input for col and a pre-defined custom palette for palette in tm_lines(). This also generates a legend automatically.

    library(bcmaps)
    library(dplyr)
    library(sf)
    library(tmap)
    
    ha_outline <- ha_outline <- bcmaps::health_ha() |> 
      rename_with(tolower, everything())
    
    pal <- c("#3891A7", "#C3860D", "#C42E2E", "#67A63C", "#914FAB")
    
    tm_shape(ha_outline) +
      tm_polygons() +
    tm_shape(ha_outline %>% st_cast("MULTILINESTRING")) +
      tm_lines(col = "hlth_authority_name", lwd = 2, palette = pal)
    
    

    enter image description here