R / ggplot2 中有没有办法重新排序图例以匹配它的行位置?

Is there a way in R / ggplot2 of re-ordering the legend to match it's line position?

在 R/ggplot2 中有没有办法重新排序图例以匹配它的行位置?

所以在这个例子中,蓝色非黑色素瘤皮肤癌将在图例中排在首位。

all_nhs_data <- read_csv("https://www.opendata.nhs.scot/dataset/c2c59eb1-3aff-48d2-9e9c-60ca8605431d/resource/3aef16b7-8af6-4ce0-a90b-8a29d6870014/download/opendata_inc9418_hb.csv")

borders_hb_cncr <- all_nhs_data %>% 
  filter(HB == "S08000016") %>% 
  select(CancerSite, Sex, Year, IncidencesAllAges, CrudeRate)

individual_viz <- borders_hb_cncr %>% 
  filter(CancerSite != "All cancer types") %>% 
 filter(case_when(
   IncidencesAllAges >=50 & Year == 2018 ~ Sex == "All",
   TRUE ~ Sex == "All" & IncidencesAllAges >50
             )) %>%  
  ggplot() +
  aes(x = Year, y = IncidencesAllAges, group = CancerSite, colour = CancerSite) +
  geom_line()

我的第一直觉是让 CancerSite 成为一个因素,并按照您想要的方式在级别语句中对其进行排序。可能是通过 2018 年 CancerSite 的价值来实现它的一种方法,这将允许您跨绘图排列重用代码。但是为此,我只是将其转换为因子。它确实改变了原来的颜色。但您可以手动操作它们。

borders_hb_cncr %>% 
filter(CancerSite != "All cancer types") %>% 
filter(case_when(
    IncidencesAllAges >=50 & Year == 2018 ~ Sex == "All",
    TRUE ~ Sex == "All" & IncidencesAllAges >50)) %>%  
mutate(CancerSite = factor(CancerSite, 
                    levels = c("Non-melanoma skin cancer", "Basal cell carcinoma of the skin",
                                "Breast", "Colon", "Colorectal cancer", 
                                "Squamous cell carcinoma of the skin", 
                                "Trachea, bronchus and lung"))) %>%
ggplot() +
aes(x = Year, y = IncidencesAllAges, colour = CancerSite) +
geom_line()

forcats 包(tidyverse 套件的一部分)有一个名为 fct_reorder2 的函数,专用于此类情况。

fct_reorder2 中的默认函数是 last2(),当按 x 排序时,它根据 y (IncidencesAllAges) 的最后一个值重新排序一个因子 (CancerSite) (Year)。见最后的例子 here.

library(tidyverse)

borders_hb_cncr %>% 
  filter(CancerSite != "All cancer types",
         case_when(
           IncidencesAllAges >=50 & Year == 2018 ~ Sex == "All",
           TRUE ~ Sex == "All" & IncidencesAllAges >50
         )) %>%  
  ggplot() +
  aes(x = Year, 
      y = IncidencesAllAges, 
      group = CancerSite, 
      colour = fct_reorder2(CancerSite, 
                            Year, 
                            IncidencesAllAges)) +
  geom_line() +
  labs(colour = 'Cancer Site')