I'm following an example from Geocomputation with R
in Chapter 4, section 4.2.6. In the example from the book, the map of New Zealand that has the average elevation in the polygons/regions has a nice, compact legend that is easy to read and placed automatically in the upper left corner. See the image output from the book below.
Here is the link to the section of the book: https://geocompr.robinlovelace.net/spatial-operations.html
When I execute the same code to try to duplicate this map, the image that is produced for me has a very distorted legend with text that is also small. I've tried adjusting my Plots pane, exporting the image in multiple formats, etc., and the legend is still distorted like this.
The code below is what I am executing:
library(sf)
library(spData)
library(tmap)
# Summarize nz regions by average elevation
nz_agg = aggregate(x = nz_height, by = nz, FUN = mean)
# Map nz regions colored by average elevation
tm_shape(nz) +
tm_polygons() +
tm_shape(nz_agg) +
tm_polygons(col = "elevation")
A couple months ago, when I produced tmaps with legends, I wasn't having this issue. I don't know if some setting has been changed or updated that I don't know about. The version of R that I'm using is 4.1.2 (2021-11-01) -- "Bird Hippie".