[英]tmap: remove white space to the right of the legend.outside
I got this plot我得到了这个 plot
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.我尝试使用outer.margins
删除它,但我的试验都没有奏效。
Any suggestions on how this can be done?关于如何做到这一点有什么建议吗?
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.在 R 中修改图形的一般策略是首先确定三种绘图范例中的哪一种在起作用,然后制作一个新的 function,这只是对代码的一个小修改。 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.查看tm_layout
的代码,它似乎主要是基本图形的图形参数替换,但这只是一个猜测。 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:然后我进一步猜测 legend.width 是要修改的参数(我选择 0.2 作为起点)但我收到了警告消息:
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警告信息: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 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.所以注意我再次尝试的警告,这次改变了legend.outside.size
参数。 It did move the plot location over to the right but I was getting a different warning with that parameter set to 0.15:它确实将 plot 位置移到了右边,但我收到了一个不同的警告,该参数设置为 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.标签的大小已调整为 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.增加 legend.width(tm_layout 的参数)以使图例更宽,从而使标签更大。 Map saved to /home/david/Downloads/diff_map3.png Resolution: 1000 by 700 pixels Size: 6.666667 by 4.666667 inches (150 dpi) Map 保存到 /home/david/Downloads/diff_map3.png 分辨率:1000 x 700 像素尺寸:6.666667 x 4.666667 英寸 (150 dpi)
See if the result is more to your liking.看看结果是否更符合您的喜好。 (t does not appear to me that the legend labels are too wide. (在我看来,图例标签不会太宽。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.