I am having difficulty creating a map with scale_fill_manual
to fill regions on the map based on some property in the region. I am using tidyterra
with terra
and the tidyverse
packages. I am unaware of methods for writing self-contained map files in r, so I provide a link to download a map file that is suitable for demonstrating the issue I am having (from
Alternatively, if I name the values
vector argument like this:
ggplot(sp_map)
geom_spatvector(aes(fill = Mu))
facet_wrap(~Process)
geom_spatvector(data = subset(map_file, !map_file$BCR %in% sp_map$BCR), fill = "white", size = 0.1)
scale_fill_manual(values = c("< 0.4"="#a50026",
"-0.4 : -0.3"="#d73027",
"-0.3 : -0.2"="#f46d43",
"-0.2 : -0.1"="#fdae61",
"-0.1 : -0.05"="#fee090",
"-0.05 : 0.05"="#ffffbf",
"0.05 : 0.1"="#e0f3f8",
"0.1 : 0.2"="#abd9e9",
"0.2 : 0.3"="#74add1",
"0.3 : 0.4"="#4575b4",
"> 0.4"="#313695"),
name = "Spice", drop = F)
theme_minimal()
theme(axis.title = element_blank())
The order of the colours is respected among factor levels but not in the legend. Furthermore, still not all levels are represented in the legend. I am stumped. The two attempts here are my most salient efforts but I have tried reordering in another factor
call, remove labels, changing labels, so on...
Any guidance here would be very much appreciated.
CodePudding user response:
For posterity, I'll post the answer to resolve the two issues that arose in my code.
In brief, although the factor levels were defined and ordered in the data object, it seems like the spatvector
map object converted all the factors back into characters. To fix this, I converted the argument to fill
back into a factor in place in the ggplot call. This allowed the drop=F
argument and the order to be respected.
ggplot(sp_map)
geom_spatvector(aes(fill = factor(Mu, levels = c("< 0.4", "-0.4 : -0.3", "-0.3 : -0.2", "-0.2 : -0.1", "-0.1 : -0.05", "-0.05 : 0.05",
"0.05 : 0.1", "0.1 : 0.2", "0.2 : 0.3", "0.3 : 0.4", "> 0.4"))))
facet_wrap(~Process)
geom_spatvector(data = subset(map_file, !map_file$BCR %in% sp_map$BCR), fill = "white", size = 0.1)
scale_fill_manual(values = c("< 0.4"="#a50026",
"-0.4 : -0.3"="#d73027",
"-0.3 : -0.2"="#f46d43",
"-0.2 : -0.1"="#fdae61",
"-0.1 : -0.05"="#fee090",
"-0.05 : 0.05"="#ffffbf",
"0.05 : 0.1"="#e0f3f8",
"0.1 : 0.2"="#abd9e9",
"0.2 : 0.3"="#74add1",
"0.3 : 0.4"="#4575b4",
"> 0.4"="#313695"),
name = "Spice", drop = F)
theme_minimal()
theme(axis.title = element_blank())
Generates:
I don't know why the spatvector has this behaviour, whether I am misusing the package or made some error somewhere. In any event, hopefully this is useful someone else down the road!