1

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.

enter image description here

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".

EastBeast
  • 89
  • 7
  • Hi @EastBeast, For me everything works (i.e. with your code I get the first image of your question = the book image). Like you, I am using `R 4.1.2.` If it helps, here are the package versions I use: `sf 1.0.5`, `tmap 3.3.2` and `spData 2.0.1`. Cheers – lovalery Dec 31 '21 at 17:43
  • Thanks for highlighting the package versions. I forgot to include that. I'm also using `sf 1.0.5`, `tmap 3.3.2` and `spData 2.0.1`. I still get the same results of the distorted legend. – EastBeast Dec 31 '21 at 17:54
  • Difficult for me to help you because I can't reproduce your problem. Maybe a hint, try opening a new session of R and loading only the 3 packages needed to execute these few lines of code and see what it does... – lovalery Dec 31 '21 at 17:59

1 Answers1

1

So, it wasn't a problem with my R version or the versions of the R packages I am using.

I use a laptop and have a couple of different locations where I use it for work. One location has multiple monitors; the other does not. I didn't notice it until researching this further, but when I plug in/unplug my monitors, sometimes it changes the resolution settings on my computer. Apparently, it wasn't enough for me to notice on my laptop until attempting to run this code. So, the cause of the distortion was due to changes on my computer that I was unaware of that happened automatically. I'll have to keep an eye on this.

user438383
  • 5,716
  • 8
  • 28
  • 43
EastBeast
  • 89
  • 7