title: Merging countries/provinces in shapefile for plotting with ggplot2
body: <p>I am trying to plot some provincial data about Canada on a map. However, some of my data is aggregated into regions so I need to combine some provinces on my map. I am aware the provinces in my data overlap with some of the regions (Alberta is part of the Prairies), but this is what I have.</p>
  
  <p>Example data I want to plot:</p>
  
  <pre><code>data_to_plot &lt;- data.frame(province = c("Alberta", "Atlantic Canada", "British Columbia", 
                                          "Ontario", "Prairies", "Quebec"),
                             data = runif(6, 1E6, 1E8))
  </code></pre>
  
  <p>The shapefile for Canada with provinces can be downloaded <a href="https://www.arcgis.com/home/item.html?id=dcbcdf86939548af81efbd2d732336db" rel="nofollow noreferrer">here</a>.</p>
  
  <p>What I tried so far:</p>
  
  <pre><code>library(tidyverse)
  library(rgdal)
  
  # Import the shape file
  shapefile &lt;- readOGR("[path to shape file]", "Canada")
  shapefile_df &lt;- fortify(shapefile, region = "NAME")
  shapefile_df$id[shapefile_df$id == "Yukon Territory"] &lt;- "Yukon"
  
  # Replace `id` with new region name, where applicable
  shapefile_df &lt;- shapefile_df %&gt;%
    mutate(id = case_when(id %in% c("New Brunswick", "Nova Scotia", "Prince Edward Island") ~ "Atlantic Canada",
                          id %in% c("Saskatchewan", "Manitoba") ~ "Prairies",
                          TRUE ~ id))
  
  # Merge map data with data to plot
  map.data &lt;- full_join(shapefile_df, data_to_plot, by = c("id" = "province"))
  
  # Plot the map
  ggplot(map.data) +
    geom_polygon(aes(x = long, y = lat, group = id, fill = data),
                 size = 0, alpha = 0.9) +
    geom_path(aes(x = long, y = lat, group = group),
              color = "grey", size = 0.5, alpha = 0.5)
  </code></pre>
  
  <p>As you can see below, this creates a mess. Preferably I would also remove any borders between provinces within the same region. I admit I don't know much about shape files or GIS, so please let me know if what I want to do is impossible.</p>
  
  <p><a href="https://i.stack.imgur.com/xEc76.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xEc76.png" alt="example_plot"></a></p>