简体   繁体   English

scale_fill_manual 因子级别、颜色或顺序被忽略

[英]scale_fill_manual factor levels, colours, or order ignored

I am having difficulty creating a map with scale_fill_manual to fill regions on the map based on some property in the region.我很难用scale_fill_manual创建一个 map 以根据区域中的某些属性填充 map 上的区域。 I am using tidyterra with terra and the tidyverse packages.我将tidyterraterratidyverse包一起使用。 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 Birds Canada ).我不知道在 r 中编写独立的 map 文件的方法,因此我提供了一个链接来下载适合演示我遇到的问题的 map 文件(来自Birds Canada )。

The problem appears as though the factor levels were unordered or ordered incorrectly.问题似乎是因子水平未排序或排序不正确。 Similar to these posts here and here .类似于此处此处的这些帖子。 However, I am explicitly naming factors created by cut , which has an argument to order the factor output. Here is a link to download the 24 MB zip file with the map.但是,我明确命名了由cut创建的因子,它有一个参数来订购因子 output。这是一个链接,用于下载 24 MB zip 文件和 map。

library(terra)
library(tidyterra)
library(tidyverse)

map_file <- vect("~/Downloads/BCR_Terrestrial/BCR_Terrestrial_master.shp") %>%
  project("+proj=lcc +lat_0=49 +lon_0=-95 +lat_1=49 +lat_2=77 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs") %>% 
  subset(., !(.$OBJECTID %in% c(1, 354, 371, 372)) & .$COUNTRY != "MEXICO")

set.seed(123)

properties <- tibble(
  Mean = rnorm(9, mean = 0, sd = 0.25),
  BCR = rep(c(6,11,17),3),
  Process = c(1,1,1,2,2,2,3,3,3)) %>% 
  mutate(
    Mu = cut(Mean, breaks = c(-Inf, -0.4, -0.3, -0.2, -0.1, -0.05, 0.05, 0.1, 0.2, 0.3, 0.4, Inf),
             labels = 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"),
             ordered_result = T))

sp_map <- merge(subset(map_file, map_file$BCR %in% properties[["BCR"]]), properties)

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("#a50026",
                               "#d73027",
                               "#f46d43",
                               "#fdae61",
                               "#fee090",
                               "#ffffbf",
                               "#e0f3f8",
                               "#abd9e9",
                               "#74add1",
                               "#4575b4",
                               "#313695"),
                    name = "Spice", drop = F)+
  theme_minimal()+
  theme(axis.title = element_blank())

Running this code, you will see that the colours in the map legend are ordered correctly but the factor levels themselves are not.运行此代码,您将看到 map 图例中的颜色顺序正确,但因子水平本身却不正确。 Also, the drop = F argument is ignored (not every level of the factor is in the legend).此外,忽略drop = F参数(并非因子的每个级别都在图例中)。 因素排序不正确

Alternatively, if I name the values vector argument like this:或者,如果我这样命名values向量参数:

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...这里的两次尝试是我最突出的努力,但我尝试在另一个factor调用中重新排序、删除标签、更改标签等等……

Any guidance here would be very much appreciated.非常感谢这里的任何指导。

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.简而言之,虽然因子级别在数据 object 中定义和排序,但似乎spatvector map object 将所有因子转换回字符。 To fix this, I converted the argument to fill back into a factor in place in the ggplot call.为了解决这个问题,我将参数fill转换回 ggplot 调用中的一个因子。 This allowed the drop=F argument and the order to be respected.这允许drop=F参数和顺序得到遵守。

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.我不知道为什么 spatvector 有这种行为,是我误用了 package 还是在某处犯了一些错误。 In any event, hopefully this is useful someone else down the road!无论如何,希望这对以后的其他人有用!

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

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