简体   繁体   English

R tmap 动态修改图例以防止图例值重叠

[英]R tmap modify legends dynamically to prevent overlapping legend values

Is there a way to prevent overlapping values in legends in tmap dynamically?有没有办法动态防止 tmap 中图例中的重叠值?

Take this example:拿这个例子:

library(tmap)
data("World")
tm_shape(World) +
tm_polygons("HPI", n=7)

The legend does not make it clear what category values of 15, 20, 25, 30, 35 or 40 belong to.图例没有说明 15、20、25、30、35 或 40 属于哪个类别值。

One solution would be this:一种解决方案是这样的:

 tm_shape(World) +
 tm_polygons("HPI", n=7,
   labels = c("0 to 15", ">15 to 20", ">20 to 25", ">25 to 30", ">30 to 35", ">35 to 40", ">40 to 45"))

However, you will obviously have to change this manually every time you have different n values for the number of categories.但是,每次您为类别数设置不同的n值时,您显然都必须手动更改此设置。

Can a similar output be generated automatically no matter how categories are used in a map?无论地图中如何使用类别,是否可以自动生成类似的输出?

The following code creates a square bracket from a closed interval (ie >=) and a "simple" bracket for an open interval (ie <).以下代码从闭区间(即 >=)创建方括号,并为开区间(即 <)创建“简单”括号。 Is this a reasonable solution?这是一个合理的解决方案吗? You can also read more details at the legend.format argument.您还可以在legend.format参数中阅读更多详细信息。

library(tmap)
#> Warning: replacing previous import 'sf::st_make_valid' by
#> 'lwgeom::st_make_valid' when loading 'tmap'
data("World")
tm_shape(World) +
  tm_polygons("HPI", n = 7, legend.format = list(scientific = TRUE, format = "f"))

Created on 2020-03-03 by the reprex package (v0.3.0)reprex 包(v0.3.0) 于 2020 年 3 月 3 日创建

我设法通过使用以下方法使其工作:

legend.format = list(format = "f", text.separator = "<"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM