It is not an elegant solution but at least it gives some result.
I added aes(fill="d2")
in geom_bar()
and removed fill="red"
. Then I added separate scales for line and for bars. Then in theme()
I removed grey background from legend entry.
To ensure that d1 in legend is shown before d2 in scale_colour_manual(" ")
there should be extra space between quotes ("longer" name).
To keep legend keys in one line, legend.box="horizontal"
added to theme()
ggplot(df, aes(Date)) +
geom_bar(aes(y = d2,fill="d2"), stat="identity") +
geom_line(aes(y = d1, group = 1, color = "d1")) +
scale_colour_manual(" ", values=c("d1" = "blue", "d2" = "red"))+
scale_fill_manual("",values="red")+
theme(legend.key=element_blank(),
legend.title=element_blank(),
legend.box="horizontal")
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…