无法删除 tmap 数据中的因素

问题描述

我开始熟悉在映射中使用 R 并且我目前正在使用 tmap 包。对数据进行子集化时遇到问题。我可以使用 tidyverse 语法毫无问题地选择各种子集,但我无法删除未使用的级别以确保图例保持合理。

#// load library and data
library(tidyverse)
library(tmap)
data("World")

#// subset data to only include Africa
tm_africa <- filter(World,continent == "Africa") 
tm_shape(tm_africa) + tm_polygons("name")
#> Warning: Number of levels of the variable "name" is 177,which is
#> larger than max.categories (which is 30),so levels are combined. Set
#> tmap_options(max.categories = 177) in the layer function to show all levels.
#> Some legend labels were too wide. These labels have been resized to 0.62,0.45,0.54,0.59,0.50,0.62,0.61,0.47,0.46,0.63. Increase legend.width (argument of tm_layout) to make the legend wider and therefore the labels larger.


#// remove unused levels for proper legends
#// output suggests levels are indeed dropped
forcats::fct_drop(tm_africa$name)
#>  [1] Angola               Burundi              Benin               
#>  [4] Burkina Faso         Botswana             Central African Rep.
#>  [7] Cote d'Ivoire        Cameroon             Dem. Rep. Congo     
#> [10] Congo                Djibouti             Algeria             
#> [13] Egypt                Eritrea              Ethiopia            
#> [16] Gabon                Ghana                Guinea              
#> [19] Gambia               Guinea-Bissau        Eq. Guinea          
#> [22] Kenya                Liberia              Libya               
#> [25] Lesotho              Morocco              Madagascar          
#> [28] Mali                 Mozambique           Mauritania          
#> [31] Malawi               Namibia              Niger               
#> [34] Nigeria              Rwanda               W. Sahara           
#> [37] Sudan                S. Sudan             Senegal             
#> [40] Sierra Leone         Somaliland           Somalia             
#> [43] Swaziland            Chad                 Togo                
#> [46] Tunisia              Tanzania             Uganda              
#> [49] South Africa         Zambia               Zimbabwe            
#> 51 Levels: Algeria Angola Benin Botswana Burkina Faso Burundi ... Zimbabwe

#// check if levels are removed
#// still listing countries from outside Africa
head(levels(tm_africa$name))
#> [1] "Afghanistan" "Albania"     "Algeria"     "Angola"      "Antarctica" 
#> [6] "Argentina"

#// plot again to verify levels have not changed
tm_shape(tm_africa) + tm_polygons("name")
#> Warning: Number of levels of the variable "name" is 177,0.63. Increase legend.width (argument of tm_layout) to make the legend wider and therefore the labels larger.

reprex package (v0.3.0) 于 2021 年 1 月 18 日创建

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...