rggplot2plottmap

How to make a frame around titles in R?


I am making plots using the tmap package. I was wondering if I could draw a frame around each plot's title. I looked into tm_layout function and did not manage to find anything relevant.

The below is my code. When you run this code, you will get the image embedded.

# plot each map
tm1 <- tm_shape(boros) +
  tm_fill(col = 'grey94') +
  tm_borders() +
tm_shape(lci_long_sf %>% filter(Indicator == 'Diversity of Commerce')) +
  tm_polygons(col = 'prop_decile', 
              palette = "viridis",
              style = 'cat',
              title = 'Decile') +
  tm_legend(show = F) +
  tm_compass(type = "arrow", position = c('left', 'bottom'), size = 1) +
  tm_scale_bar(text.size = 0.45, position = c("left", "bottom")) +
 
  tm_layout(frame = F,
            main.title = 'Diversity of Commerce',
            main.title.position = c('center', 'top'),
            main.title.size = 1)


  
tm2 <- tm_shape(boros) +
  tm_fill(col = 'grey94') +
  tm_borders() +
tm_shape(lci_long_sf %>% filter(Indicator == 'Diversity of Education')) +
  tm_polygons(col = 'prop_decile', 
              palette = "viridis",
              style = 'cat',
              title = 'Decile') +
  tm_legend(show = F) +
  tm_compass(type = "arrow", position = c('left', 'bottom'), size = 1) +
  tm_scale_bar(text.size = 0.45, position = c("left", "bottom")) +
  tm_layout(frame = F,
            main.title = 'Diversity of Education',
            main.title.position = c('center', 'top'),
            main.title.size = 1)
  
  
  
tm3 <- tm_shape(boros) +
  tm_fill(col = 'grey94') +
  tm_borders() +
tm_shape(lci_long_sf %>% filter(Indicator == 'Diversity of Entertainment')) +
  tm_polygons(col = 'prop_decile', 
              palette = "viridis",
              style = 'cat',
              title = 'Decile') +
  tm_legend(show = F) +
  tm_compass(type = "arrow", position = c('left', 'bottom'), size = 1) +
  tm_scale_bar(text.size = 0.45, position = c("left", "bottom")) +
  tm_layout(frame = F,
            main.title = 'Diversity of Entertainment',
            main.title.position = c('center', 'top'),
            main.title.size = 1)  

  
  
tm4 <- tm_shape(boros) +
  tm_fill(col = 'grey94') +
  tm_borders() +
tm_shape(lci_long_sf %>% filter(Indicator == 'Diversity of Living')) +
  tm_polygons(col = 'prop_decile', 
              palette = "viridis",
              style = 'cat',
              title = 'Decile') +
  tm_legend(show = F) +
  tm_compass(type = "arrow", position = c('left', 'bottom'), size = 1) +
  tm_scale_bar(text.size = 0.45, position = c("left", "bottom")) +
  tm_layout(frame = F,
            main.title = 'Diversity of Living',
            main.title.position = c('center', 'top'),
            main.title.size = 1)

  
tm5 <- tm_shape(boros) +
  tm_fill(col = 'grey94') +
  tm_borders() +
tm_shape(lci_long_sf %>% filter(Indicator == 'Diversity of Healthcare')) +
  tm_polygons(col = 'prop_decile', 
              palette = "viridis",
              style = 'cat',
              title = 'Decile') +
  tm_legend(show = F) +
  tm_compass(type = "arrow", position = c('left', 'bottom'), size = 1) +
  tm_scale_bar(text.size = 0.45, position = c("left", "bottom")) +
  tm_layout(frame = F,
            main.title = 'Diversity of Healthcare',
            main.title.position = c('center', 'top'),
            main.title.size = 1)  

# legend
legend <- tm_shape(lci_long_sf) +
  tm_polygons('prop_decile',
              palette = 'viridis',
              title = 'Proportion',
              style = 'cat') +
  tm_layout(legend.only = T,
            legend.position = c('center', 'center'), 
            legend.height = 3.5,
            legend.width = 4,
            asp = 0.1)


t = tmap_arrange(tm1, tm2, tm3, tm4, tm5, legend, ncol=2)

t

enter image description here

And this is what I mean by drawing a frame. enter image description here

Could anyone help? Thank you.


Solution

  • Instead of using main.title, use panel.show = TRUE with appropriatepanel.labels. Also make sure frame = TRUE. For example, your first plot's tm_layout would be:

      tm_layout(frame = TRUE,
                panel.show = TRUE,
                panel.labels = 'Diversity of Healthcare')
    

    If you change all the tm_layout to this format, you get:

    enter image description here