rggplot2plotspatialtmap

How to increase the colourbar size in tmap package in R?


I am making a map using tmap package in R. I am trying to increase the colour bar within the map but it is not working.

You can find the data in the following link:

London borough boundary data london boundary data

London amenity entropy data entropy data

London road data road data

The below is my code:

# read in london boundary data
boros <- st_read('boros.geojson') %>% st_transform(., 27700)

# read in road data
edges <- st_read('london_all.gpkg', layer = "edges") %>% st_transform(., 27700)

# entropy data
london_entropy_iso <- st_read('london_entropy_iso_500.geojson') %>% st_transform(27700)

#plot the entropy in London
london_entropy_map <- 
  tm_shape(boros) + 
  tm_polygons(col = NA, alpha = 0.5) +
  
  tm_shape(edges) + 
  tm_lines(col = "black",
           size = 0.5) + 
  
  tm_shape(london_entropy_iso) + 
  tm_dots(col = 'entropy',
          palette = 'viridis',
          size = 0.02,
          style = 'cont',
          title = 'Entropy') +
  tm_compass(type = "arrow", position = c('left', 'bottom'), size = 2) +
  tm_scale_bar(text.size = 0.9, position = c("left", "bottom")) +
  tm_layout(legend.position = c("right", "bottom"),
            main.title= "Amenity Diversity Entropy within a 500m Isodistance", 
            main.title.position = c('center', 'top'),
            main.title.size = 3,
            frame = FALSE,  # Remove the box surrounding the map
            inner.margins = 0.1)

# Increase the size of the legend title
london_entropy_map <- london_entropy_map + tm_legend(title.size = 1.5)

# save map
tmap_save(london_entropy_map, 
          filename = "london_entropy_iso_500_map.png",
          width = 16,
          height = 20,
          dpi = 300)

This code results in the image attached. As you can see, the colour bar on the right bottom corner is too small. enter image description here I also added 'legend.width' and 'legend.height' within tm_layout() but it is not working.

How can I increase the size of the colour bar? Could anyone help? Thank you.


Solution

  • Simply use legend.text.size to adjust the size of the color scale for your needs:

    library(tmap)
    data(World)
    
    tm_shape(World) +
      tm_fill("life_exp", style = "cont", breaks = seq(0, 100, by = 10)) +
      tm_layout(legend.text.size = 0.5)
    

    
    tm_shape(World) +
      tm_fill("life_exp", style = "cont", breaks = seq(0, 100, by = 10)) +
      tm_layout(legend.text.size = 1)
    

    Created on 2023-08-14 with reprex v2.0.2