1

Does anyone have experience of changing the order of legend elements when using tm_fill?

I created a map using tm_polygon and tried breaks and labels to change the order. Both of them failed.

Please see examples below. gov_reg_update is the sf object.

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. 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')) 

enter image description here

Thanks.

Yabin Da
  • 553
  • 5
  • 11

1 Answers1

4

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. For simplicity I only selected names starting with A here:

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.

enter image description here

mgrund
  • 1,415
  • 8
  • 10
  • 1
    Problem solved. I tried `factor` before I posted the question. Somehow it did not work. – Yabin Da Jul 29 '21 at 14:30
  • It's anyone's guess how many time this question has been asked and answered. Someone should curate the results from a search on `[r] order factor levels. A start might be with `[r] order plot (levels OR labels) factor`, but I think that might be too narrow. – IRTFM Aug 13 '21 at 01:07