Plotting a map with zoom-in mini maps

Hi Colleagues.

I’m trying to plot the map of one region with a “zoom-in” map of its three municipalities. I was able to generate the basic map of the region and the municipalities. However, I cannot figure out how to do the “zoom-in” effect. The following are my codes:

# Load the shapefiles
shapefile <- read.csv
philippines_sf <- read_sf(here("shapefiles", "gadm41_PHL_2.shp"))
palawan_sf <- 
  read_sf(here("shapefiles", "gadm41_PHL_2.shp")) %>% 
  clean_names() %>% 
  filter(name_1 == "Palawan") 

# Plot the Province (Palawan map) 
palawan_plot <- ggplot() +
  geom_sf(data = palawan_sf, fill = "seagreen2", color = "black") +
  coord_sf(default_crs = NULL) + 
  theme_void() +
  labs(title = "Province of Palawan")

# Plot the three municipalities/city
Balabac_sf<- 
  read_sf(here("shapefiles", "gadm41_PHL_2.shp")) %>% 
  clean_names() %>% 
  filter(name_1 == "Palawan") %>% 
  filter(name_2 == "Balabac") %>%   
  select(name_2, geometry) 


balabac_plot <- ggplot() +
  geom_sf(data = Balabac_sf, fill = "palegreen3", color = "black") +
  coord_sf(default_crs = NULL) + 
  theme_void() +
  labs(title = "Municipality of Balabac")


PuertoPrincesa_sf <- 
  read_sf(here("shapefiles", "gadm41_PHL_2.shp")) %>% 
  clean_names() %>% 
  filter(name_1 == "Palawan") %>% 
 filter(name_2 == "Puerto Princesa City") %>%   
 select(name_2, geometry)


PP_plot <- ggplot() +
  geom_sf(data = PuertoPrincesa_sf, fill = "darkolivegreen", color = "black") +
  coord_sf(default_crs = NULL) +  # Set default_crs argument to NULL
  theme_void() +
  labs(title = "Puerto Princesa City")


Rizal_sf <- 
  read_sf(here("shapefiles", "gadm41_PHL_2.shp")) %>% 
  clean_names() %>% 
  filter(name_1 == "Palawan") %>% 
  filter(name_2 == "Rizal") %>%    #choose provinces/cities to map
  select(name_2, geometry)


Rizal_plot <- ggplot() +
  geom_sf(data = Rizal_sf, fill = "palegreen", color = "black") +
  coord_sf(default_crs = NULL) +  # Set default_crs argument to NULL
  theme_void() +
  labs(title = "Municipality of Rizal")

#Province map with "zoom-in" map of the three municipalities? 

I’m thinking of combining the plots and adding arrows or lines from the province map to the map of the three municipalities. Combining is through grid.arrange, but I can’t do the arrows or lines. I also tried “tmap” but it only generates the individual maps.
I hope you could guide me on this.
Thank you in advance.

Regards,
Echo

Dear Echo,

One possibility is to use the approach here:

Neale

1 Like

Thank you, will review this one.
Just to share, I was able to create a map with zoom-in mini map (inset map of one province) using the script below:

getData("GADM", country = "philippines", level = 3)
mymap <- fortify(mydata) 


#check range of long and lat for the limits of x and y continuous
summary(mymap$long)
summary(mymap$lat)

g1 <-  ggplot() +
  geom_blank(data = mymap, aes(x = long, y = lat)) +
  geom_map(data = mymap, map = mymap,
           aes(group = group, map_id = id),
           fill = "yellowgreen", color = "black", size = 0.1) +
  scale_x_continuous(limits = c(116, 127), expand = c(0, 0)) +   #longitude = x, latitude = y
  scale_y_continuous(limits = c(5, 22), expand = c(0, 0)) +
  theme_map()

g1

#palawan 
g2 <- ggplotGrob(
  ggplot() +
    geom_blank(data = mymap, aes(x = long, y = lat)) +
    geom_map(data = mymap, map = mymap,
             aes(group = group, map_id = id),
             fill = "yellowgreen", color = "black", size = 0.1) +
    geom_path(data = temp, aes(x = long, y = lat), size = 0.3) +
    scale_x_continuous(limits = c(116, 121), expand = c(0, 0)) + #adjust limits for width covered
    scale_y_continuous(limits = c(7, 12.4), expand = c(0, 0)) +   #adjust limits for height covered
    coord_map("polyconic") +
    theme_map() +
    theme(panel.background = element_rect(color = "black"))
)
g2

g3 <- g1 +
  annotation_custom(grob = g2, xmin = 116, xmax = 119,
                    ymin = 7, ymax = 14) +
  labs(title = "Province of Palawan, Region IV-B (MIMAROPA), Philippines") +
  theme(plot.title = element_text(hjust = 0.5, color = "black", face = "bold"))   #face - font if bold or not

g3

1 Like

Congratulations, and thanks for sharing your working code with the community!

1 Like