我正在尝试在相同的轴上制作条形图和线图,矩形显示数据可能不完整的位置,垂直线显示重要日期。
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)
该图表看起来像我想要的那样,除了图例之外,它没有显示代表两次考试变化的线条之间的颜色差异。如何使图例以橙色而不是黑色显示“宣布考试变更 1”和“考试变更 1”?