我已经有 'color' 和 'fill' 个图例。如何控制使用 geom_vline 生成的线条的颜色?

I already have 'color' and 'fill' legends. How can I control the colour of a line produced using geom_vline?

我正在尝试在同一轴上制作条形图和线图,其中矩形显示数据可能不完整的位置,垂直线显示重要日期。

library(dplyr)
library(ggplot2)
library(scales)

dates <- c("2022-01-01","2022-01-08","2022-01-15")
count_variables <- c('pass','fail')
counts <- c(30,28,26,10,11,12)

data_bar <- expand.grid(date = dates, count_variable = count_variables, stringsAsFactors = F)
data_bar$count <- counts
data_bar$date <- as.Date(data_bar$date)

ave_marks <- c(75,72,68)

data_line <- data.frame(date = dates,
                        perc = ave_marks,
                        perc_variable = 'average mark')
data_line$date <- as.Date(data_line$date)


ggplot() +
  # Rectangle covering last few days
  geom_rect(aes(xmin=max(data_bar$date)+4, xmax=max(data_bar$date), ymin=-Inf, ymax=Inf, 
                fill="Time period subject\nto reporting delays"), colour=NA, alpha = .2) +
  # Bar plot
  geom_bar(data=data_bar, aes(fill=count_variable, y=count, x=date), position="dodge", stat="identity") +
  scale_fill_manual(labels = c("Pass","Fail","Time period subject\nto reporting delays"), 
                    values = c("green","red","#e4004620")) +
  # Line plot
  geom_line(data=data_line, aes(x=date, y=perc/3, color=perc_variable)) +
  #scale_color_manual(labels = c("Pass rate"), values = c("blue")) +
  # Vertical lines
  geom_vline(aes(xintercept = as.Date("2022-01-05"),linetype = "Exam change 1 announced"),color='orange') +
  geom_vline(aes(xintercept = as.Date("2022-01-06"),linetype = "Exam change 1"),color='orange') +
  geom_vline(aes(xintercept = as.Date("2022-01-12"),linetype = "Exam change 2 announced"),color='black') +
  geom_vline(aes(xintercept = as.Date("2022-01-13"),linetype = "Exam change 2"),color='black') +
  scale_color_manual(labels = c("Pass rate","Exam change 1 announced","Exam change 1",
                                "Exam change 2 announced","Exam change 2"), 
                     values = c("blue","orange","orange","black","black")) +
  scale_linetype_manual(values=c("longdash","solid","longdash","solid"),
                        limits=c("Exam change 1 announced","Exam change 1",
                                 "Exam change 2 announced","Exam change 2"),
                        labels = c("Exam change 1 announced","Exam change 1",
                                   "Exam change 2 announced","Exam change 2")) +
  scale_y_continuous(sec.axis = sec_axis(~.*3, name = 'Percentage'),
                     labels = comma)

The code produces this chart.

除了图例之外,图表看起来是我想要的样子,图例没有显示代表两次考试变化的线条之间的颜色差异。我怎样才能让图例以橙色而不是黑色显示 'Exam change 1 announced' 和 'Exam change 1'?

实现所需结果的一个选项是使用 guide_legendoverride.aes 参数覆盖应用于线型图例的美学,它允许您为每个图例项设置颜色:

library(dplyr)
library(ggplot2)
library(scales)

ggplot() +
  # Rectangle covering last few days
  geom_rect(aes(
    xmin = max(data_bar$date) + 4, xmax = max(data_bar$date), ymin = -Inf, ymax = Inf,
    fill = "Time period subject\nto reporting delays"
  ), colour = NA, alpha = .2) +
  # Bar plot
  geom_bar(data = data_bar, aes(fill = count_variable, y = count, x = date), position = "dodge", stat = "identity") +
  scale_fill_manual(
    labels = c("Pass", "Fail", "Time period subject\nto reporting delays"),
    values = c("green", "red", "#e4004620")
  ) +
  # Line plot
  geom_line(data = data_line, aes(x = date, y = perc / 3, color = perc_variable)) +
  # scale_color_manual(labels = c("Pass rate"), values = c("blue")) +
  # Vertical lines
  geom_vline(aes(xintercept = as.Date("2022-01-05"), linetype = "Exam change 1 announced"), color = "orange") +
  geom_vline(aes(xintercept = as.Date("2022-01-06"), linetype = "Exam change 1"), color = "orange") +
  geom_vline(aes(xintercept = as.Date("2022-01-12"), linetype = "Exam change 2 announced"), color = "black") +
  geom_vline(aes(xintercept = as.Date("2022-01-13"), linetype = "Exam change 2"), color = "black") +
  scale_color_manual(
    labels = c(
      "Pass rate", "Exam change 1 announced", "Exam change 1",
      "Exam change 2 announced", "Exam change 2"
    ),
    values = c("blue", "orange", "orange", "black", "black")
  ) +
  scale_linetype_manual(
    values = c("longdash", "solid", "longdash", "solid"),
    limits = c(
      "Exam change 1 announced", "Exam change 1",
      "Exam change 2 announced", "Exam change 2"
    ),
    labels = c(
      "Exam change 1 announced", "Exam change 1",
      "Exam change 2 announced", "Exam change 2"
    )
  ) +
  scale_y_continuous(
    sec.axis = sec_axis(~ . * 3, name = "Percentage"),
    labels = comma
  ) +
  guides(linetype = guide_legend(override.aes = list(color = c("orange", "orange", "black", "black"))))