I created the following map. However, the labels in the legend don't match the data. I don't understand what goes wrong. I expected the pal colors to be ordered with the labels. So would do the data.
Example= no drought color would normally correspond to the severe drought cells on the map.
Here is a sample of my data
structure(list(gid = c(100468L, 100468L, 100468L, 100468L, 100468L,
100468L, 100468L, 100468L, 100468L, 100468L, 100475L, 100475L,
100475L, 100475L, 100475L, 100475L, 100475L, 100475L, 100475L,
100475L), country = c("Namibia", "Namibia", "Namibia", "Namibia",
"Namibia", "Namibia", "Namibia", "Namibia", "Namibia", "Namibia",
"Namibia", "Namibia", "Namibia", "Namibia", "Namibia", "Namibia",
"Namibia", "Namibia", "Namibia", "Namibia"), total_conflicts = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Year = c(2005,
2003, 2009, 2007, 2008, 2001, 2006, 1999, 2002, 2004, 1997, 2009,
2007, 2010, 2000, 2008, 2006, 1999, 2005, 2004), ycoord = c(-20.25,
-20.25, -20.25, -20.25, -20.25, -20.25, -20.25, -20.25, -20.25,
-20.25, -20.25, -20.25, -20.25, -20.25, -20.25, -20.25, -20.25,
-20.25, -20.25, -20.25), xcoord = c(13.75, 13.75, 13.75, 13.75,
13.75, 13.75, 13.75, 13.75, 13.75, 13.75, 17.25, 17.25, 17.25,
17.25, 17.25, 17.25, 17.25, 17.25, 17.25, 17.25), VCI = c(38.4215158606475,
33.233853670267, 66.8255937282856, 45.530642912938, 47.6116253045889,
43.6939180080707, 71.3801674476037, 16.7028192006625, 49.7771763434777,
29.5257619689493, 58.5738585178669, 67.0596055250901, 41.2335995894212,
60.8284931916457, 41.6004944466628, 55.5477022024301, 77.3257617216844,
13.382687486135, 37.1140933306712, 39.0084276199341), vcidrought = c(1,
1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1), vcidrought_type = structure(c(2L,
2L, 4L, 4L, 4L, 4L, 4L, 5L, 4L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
5L, 2L, 2L), .Label = c("Extreme drought", "Mild drought", "Moderate drought",
"No drought", "Severe drought"), class = "factor"), geometry = structure(list(
structure(list(structure(c(14, 14, 13.5, 13.5, 14, -20.5,
-20, -20, -20.5, -20.5), .Dim = c(5L, 2L))), class = c("XY",
"POLYGON", "sfg")), structure(list(structure(c(14, 14, 13.5,
13.5, 14, -20.5, -20, -20, -20.5, -20.5), .Dim = c(5L, 2L
))), class = c("XY", "POLYGON", "sfg")), structure(list(structure(c(14,
14, 13.5, 13.5, 14, -20.5, -20, -20, -20.5, -20.5), .Dim = c(5L,
2L))), class = c("XY", "POLYGON", "sfg")), structure(list(
structure(c(14, 14, 13.5, 13.5, 14, -20.5, -20, -20,
-20.5, -20.5), .Dim = c(5L, 2L))), class = c("XY", "POLYGON",
"sfg")), structure(list(structure(c(14, 14, 13.5, 13.5, 14,
-20.5, -20, -20, -20.5, -20.5), .Dim = c(5L, 2L))), class = c("XY",
"POLYGON", "sfg")), structure(list(structure(c(14, 14, 13.5,
13.5, 14, -20.5, -20, -20, -20.5, -20.5), .Dim = c(5L, 2L
))), class = c("XY", "POLYGON", "sfg")), structure(list(structure(c(14,
14, 13.5, 13.5, 14, -20.5, -20, -20, -20.5, -20.5), .Dim = c(5L,
2L))), class = c("XY", "POLYGON", "sfg")), structure(list(
structure(c(14, 14, 13.5, 13.5, 14, -20.5, -20, -20,
-20.5, -20.5), .Dim = c(5L, 2L))), class = c("XY", "POLYGON",
"sfg")), structure(list(structure(c(14, 14, 13.5, 13.5, 14,
-20.5, -20, -20, -20.5, -20.5), .Dim = c(5L, 2L))), class = c("XY",
"POLYGON", "sfg")), structure(list(structure(c(14, 14, 13.5,
13.5, 14, -20.5, -20, -20, -20.5, -20.5), .Dim = c(5L, 2L
))), class = c("XY", "POLYGON", "sfg")), structure(list(structure(c(17.5,
17.5, 17, 17, 17.5, -20.5, -20, -20, -20.5, -20.5), .Dim = c(5L,
2L))), class = c("XY", "POLYGON", "sfg")), structure(list(
structure(c(17.5, 17.5, 17, 17, 17.5, -20.5, -20, -20,
-20.5, -20.5), .Dim = c(5L, 2L))), class = c("XY", "POLYGON",
"sfg")), structure(list(structure(c(17.5, 17.5, 17, 17, 17.5,
-20.5, -20, -20, -20.5, -20.5), .Dim = c(5L, 2L))), class = c("XY",
"POLYGON", "sfg")), structure(list(structure(c(17.5, 17.5,
17, 17, 17.5, -20.5, -20, -20, -20.5, -20.5), .Dim = c(5L,
2L))), class = c("XY", "POLYGON", "sfg")), structure(list(
structure(c(17.5, 17.5, 17, 17, 17.5, -20.5, -20, -20,
-20.5, -20.5), .Dim = c(5L, 2L))), class = c("XY", "POLYGON",
"sfg")), structure(list(structure(c(17.5, 17.5, 17, 17, 17.5,
-20.5, -20, -20, -20.5, -20.5), .Dim = c(5L, 2L))), class = c("XY",
"POLYGON", "sfg")), structure(list(structure(c(17.5, 17.5,
17, 17, 17.5, -20.5, -20, -20, -20.5, -20.5), .Dim = c(5L,
2L))), class = c("XY", "POLYGON", "sfg")), structure(list(
structure(c(17.5, 17.5, 17, 17, 17.5, -20.5, -20, -20,
-20.5, -20.5), .Dim = c(5L, 2L))), class = c("XY", "POLYGON",
"sfg")), structure(list(structure(c(17.5, 17.5, 17, 17, 17.5,
-20.5, -20, -20, -20.5, -20.5), .Dim = c(5L, 2L))), class = c("XY",
"POLYGON", "sfg")), structure(list(structure(c(17.5, 17.5,
17, 17, 17.5, -20.5, -20, -20, -20.5, -20.5), .Dim = c(5L,
2L))), class = c("XY", "POLYGON", "sfg"))), class = c("sfc_POLYGON",
"sfc"), precision = 0, bbox = structure(c(xmin = 13.5, ymin = -20.5,
xmax = 17.5, ymax = -20), class = "bbox"), crs = structure(list(
input = "WGS 84", wkt = "GEOGCRS[\"WGS 84\",\n DATUM[\"World Geodetic System 1984\",\n ELLIPSOID[\"WGS 84\",6378137,298.257223563,\n LENGTHUNIT[\"metre\",1]]],\n PRIMEM[\"Greenwich\",0,\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n CS[ellipsoidal,2],\n AXIS[\"latitude\",north,\n ORDER[1],\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n AXIS[\"longitude\",east,\n ORDER[2],\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n ID[\"EPSG\",4326]]"), class = "crs"), n_empty = 0L)), row.names = c(8L,
10L, 11L, 13L, 14L, 15L, 16L, 17L, 20L, 21L, 216L, 220L, 222L,
225L, 227L, 229L, 230L, 235L, 236L, 237L), class = c("sf", "data.frame"
), sf_column = "geometry", agr = structure(c(gid = NA_integer_,
country = NA_integer_, total_conflicts = NA_integer_, Year = NA_integer_,
ycoord = NA_integer_, xcoord = NA_integer_, VCI = NA_integer_,
vcidrought = NA_integer_, vcidrought_type = NA_integer_), .Label = c("constant",
"aggregate", "identity"), class = "factor"))
Here is my code:
labels = c("No drought", "Mild drought", "Moderate drought",
"Severe drought", "Extreme drought")
tm_shape(test) +
tm_polygons(col="vcidrought_type",
pal =c("#5DC863FF", "#21908CFF", "#3B528BFF", "#440154FF", "#000004FF"),
labels = labels,
title="Drought types") +
tm_layout(main.title = "VCI drought type in Sub-Saharan Africa 1997-2010",
main.title.position = "center", frame= FALSE) +
tm_shape(shp_union) +
tm_symbols(col = "red", border.col = "white", size = "total_conflicts")