9  Legends

library(sf)
#> Linking to GEOS 3.10.2, GDAL 3.4.1, PROJ 8.2.1; sf_use_s2() is TRUE
library(stars)
#> Loading required package: abind
library(tmap)
ei_elev = read_stars("data/easter_island/ei_elev.tif")
tm_shape(ei_elev) +
  tm_raster(col.scale = tm_scale(values = "geyser"),
            col.legend = tm_legend(title = "Elevation (m asl)"))

tm_shape(ei_elev) +
  tm_raster(col.scale = tm_scale(values = "geyser"),
            col.legend = tm_legend(title = "Elevation (m asl)",
                                   reverse = TRUE))

tm_shape(ei_elev) +
  tm_raster(col.scale = tm_scale(values = "geyser"),
            col.legend = tm_legend(title = "Elevation (m asl)",
                                   position = tm_pos_in("right", "bottom")))

tm_shape(ei_elev) +
  tm_raster(col.scale = tm_scale(values = "geyser"),
            col.legend = tm_legend(title = "Elevation (m asl)",
                                   position = tm_pos_in("right", "bottom"),
                                   bg.color = "gray", bg.alpha = 0.5))

tm_shape(ei_elev) +
  tm_raster(col.scale = tm_scale_continuous(values = "geyser"),
            col.legend = tm_legend(title = "Elevation (m asl)",
                                   orientation = "landscape",
                                   position = tm_pos_out("center", "top",
                                                         "center")))
#> [plot mode] fit legend/component: Some legend items or map compoments do not
#> fit well, and are therefore rescaled.
#> ℹ Set the tmap option `component.autoscale = FALSE` to disable rescaling.

tm_shape(ei_elev) +
  tm_raster(col.scale = tm_scale(values = "geyser"),
            col.legend = tm_legend(show = FALSE))