0

I got this plot

enter image description here

using this script

library(raster)
library(tmap)
library(classInt)

download.file("https://github.com/mtennekes/tmap/files/5500015/Difference.tif.zip",
              "Difference.tif.zip")

unzip("Difference.tif.zip", "Difference.tif")

diff <- raster("Difference.tif")

diff_values <- getValues(diff)

diff_values_below0 <- diff_values[diff_values < 0]
diff_values_above0 <- diff_values[diff_values > 0]

classes1 <- classIntervals(diff_values_below0, n = 4, style = "fisher")
classes2 <- classIntervals(diff_values_above0, n = 4, style = "fisher")

all_classes <- c(classes1$brks, classes2$brks)


diff_map<- 
  tm_shape(diff) + 
  tm_raster(midpoint = 0, 
            breaks = all_classes,
            palette = "RdBu")+
  tm_layout(legend.outside = TRUE)

tmap_save(diff_map, "diff_map.png", width=1000, height=700,dpi = 150)

As you can see, there is white space to the right of the legend. I tried removing it using outer.margins but none of my trials worked.

Any suggestions on how this can be done?

shiny
  • 3,380
  • 9
  • 42
  • 79

1 Answers1

2

My compliments to you for writing a well documented question. The general strategy for modding graphics in R is to first identify which of the three plotting paradigms are in play and then make a new function that is but a minor modification of the code. A more specific strategy is to carefully read the documentation and then make mods to your parameters. I chose a middle ground because I assumed you'd already done the second one.

Looking at the code for tm_layout it appeared to be mostly substitutions of graphics parameters for base graphics, but that was just a guess. I then took a further guess that legend.width was the parameter to modify (I chose as a starting point, 0.2) but I got the warning message:

Warning message: In preprocess_gt(x, interactive = interactive, orig_crs = gm$shape.orig_crs) : legend.width controls the width of the legend within a map. Please use legend.outside.size to control the width of the outside legend

Then looking at the result in my file browser I saw .... no improvement. So heeding the warning I tried again, this time altering the legend.outside.size parameter. It did move the plot location over to the right but I was getting a different warning with that parameter set to 0.15:

Legend labels were too wide. The labels have been resized to 0.54, 0.59, 0.59, 0.59, 0.62, 0.66, 0.66, 0.60, 0.55. Increase legend.width (argument of tm_layout) to make the legend wider and therefore the labels larger. Map saved to /home/david/Downloads/diff_map3.png Resolution: 1000 by 700 pixels Size: 6.666667 by 4.666667 inches (150 dpi)

See if the result is more to your liking. (t does not appear to me that the legend labels are too wide. enter image description here

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • Thank you so much for your time and help. Thanks to the package developer, it is a combination of `legend.outside.size` and `asp` https://github.com/mtennekes/tmap/issues/522 – shiny Dec 03 '20 at 01:54
  • If you have a better answer, then do feel free to post an answer and give yourself a check. – IRTFM Dec 03 '20 at 03:17