简体   繁体   English

如何更改tmap中图例元素的顺序

[英]How to change the order of legend elements in tmap

Does anyone have experience of changing the order of legend elements when using tm_fill ?有没有人在使用tm_fill时改变图例元素的顺序?

I created a map using tm_polygon and tried breaks and labels to change the order.我使用tm_polygon创建了一个地图,并尝试使用breakslabels来更改顺序。 Both of them failed.他们俩都失败了。

Please see examples below.请参阅下面的示例。 gov_reg_update is the sf object. gov_reg_updatesf对象。

head(gov_reg_update)
Simple feature collection with 6 features and 1 field
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: 24.69451 ymin: 21.99913 xmax: 36.90871 ymax: 31.66855
Geodetic CRS:  WGS 84
# A tibble: 6 x 2
  region_gams                                                                                geometry
  <chr>                                                                            <MULTIPOLYGON [°]>
1 e-delta     (((31.22864 30.05246, 31.22419 30.07381, 31.21677 30.06491, 31.22344 30.03963, 31.22...
2 m-delta     (((31.2195 31.15309, 31.21099 31.15603, 31.20113 31.15235, 31.19361 31.1579, 31.1728...
3 m-egypt     (((30.99183 28.90639, 30.99953 28.91931, 30.99231 28.9239, 30.98534 28.91505, 30.979...
4 others      (((28.53467 27.67273, 28.46664 27.67461, 27.36089 27.67578, 27.83086 28.57875, 28.46...
5 u-egypt     (((32.8852 24.46148, 32.88283 24.45191, 32.89828 24.45578, 32.90756 24.45386, 32.915...
6 w-delta     (((30.83602 30.48546, 30.82963 30.49857, 30.83614 30.51551, 30.84493 30.52367, 30.85...

Here are the codes I used to draw the map.这是我用来绘制地图的代码。 As can be seen from the image below, the order of the legend elements does not match what I specified in breaks .从下图可以看出,图例元素的顺序与我在breaks指定的顺序不匹配。 Any ideas?有任何想法吗?

  tm_shape(gov_reg_update) +
     tm_polygons('region_gams', title = 'Regions',
                    breaks = c('u-egypt','m-egypt','e-delta','m-delta','w-delta')) 

在此处输入图片说明

Thanks.谢谢。

You can change the plot order by defining your names as factors (in your case they are ordered alphabetically).您可以通过将您的名称定义为因子来更改绘图顺序(在您的情况下,它们按字母顺序排列)。 I used an open dataset ( https://data.humdata.org/dataset/egypt-administrative-boundaries-levels-0-3 ) including the shapes of Egypt's administration levels.我使用了一个开放数据集( https://data.humdata.org/dataset/egypt-administrative-boundaries-levels-0-3 ),包括埃及行政级别的形状。 For simplicity I only selected names starting with A here:为简单起见,我在这里只选择了以 A 开头的名称:

library(tmap)
library(sf)
library(tidyverse)

regions <- st_read(dsn = "egy_admbnda_adm1_capmas_20170421/egy_admbnda_adm1_capmas_20170421.shp") %>% 
  as.tibble() %>% 
  filter(str_detect(ADM1_EN, "A")) %>% 
  mutate(ADM1_EN = factor(ADM1_EN, levels = c("Assiut", "Aswan", "Alexandria"))) %>% 
  st_as_sf()

tm_shape(regions) +
    tm_polygons("ADM1_EN")

On the left side you see the result without the mutate() line (names are ordered alphavetically), on the right after defining the new order via factors.在左侧,您会看到没有mutate()行的结果(名称按字母顺序排列),在通过因子定义新顺序后的右侧。

在此处输入图片说明

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

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