简体   繁体   English

R tmap,图例格式 - 在第一个图例键中显示单个值

[英]R tmap, legend formatting - display single value in the first legend key

I'm not sure how or if is possible to adjust the key legends in the fallowing way. 我不确定如何或是否有可能以下列方式调整关键传说。 Consider the example: 考虑这个例子:

library(tmap)
data(Europe)

my_map <- 
  tm_shape(Europe) +
  tm_polygons("well_being", textNA="Non-European countries", title="Well-Being Index") +
  tm_text("iso_a3", size="AREA", root=5) +
  tm_layout(legend.position = c("RIGHT","TOP"),
            legend.frame = TRUE)

在此输入图像描述

I tried legend.format = list(scientific = TRUE) in something like: 我尝试了legend.format = list(scientific = TRUE) ,例如:

my_map2 <- my_map +
  tm_layout(legend.format = list(scientific = TRUE))

which gives this for a legend: 这给了一个传奇:

在此输入图像描述

However, what I wish is something like: 但是,我希望的是:

在此输入图像描述

In my data, the 4 is a zero, and I was asked to make it stand out as zero alone. 在我的数据中,4是零,我被要求让它脱颖而出,仅为零。

As far as I know this is impossible by using tmap legend formatting alone - there will always be the little separator. 据我所知,单独使用tmap图例格式是不可能的 - 总会有小分隔符。 Either "to" or whatever you overwrite it with text.separator from legend.format call. 使用来自legend.format调用的text.separator “to”或覆盖它的任何内容。

What you need is one extra step in your data processing - you must create a special variable with your labels, and plot according to this. 您需要的是数据处理中的一个额外步骤 - 您必须使用标签创建一个特殊变量,并根据此绘图。 You have full control over the levels, so you can have the first one as standalone zero. 您可以完全控制级别,因此您可以将第一个作为独立的零。

For simplicity sake I am using ifelse to split into two levels (plus the NAs for Non-European countries), but you can get more fancy than that... 为简单起见,我使用ifelse分为两个级别(加上非欧洲国家的NA),但你可以得到更多的幻想......

Europe <- Europe %>%
  st_as_sf() %>%  # from sf package - makes Europe behave as a data.frame
  mutate(well_being_adj = ifelse(well_being < 5, "0", "more than five"))

my_map <- 
  tm_shape(Europe) +
  tm_polygons("well_being_adj", textNA="Non-European countries", title="Well-Being Index") +
  tm_text("iso_a3", size="AREA", root=5) +
  tm_layout(legend.position = c("RIGHT","TOP"),
            legend.frame = TRUE)
print(my_map)

在此输入图像描述

Despite being solved, another more practical alternative is to modify the levels in tm_polygons labels 尽管已经解决了,但另一个更实用的替代方法是修改tm_polygons labels的级别

tm_polygons(labels = c ("0", "more than five")

This way also serves for rasters files 这种方式也适用于栅格文件

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

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