library(tidyverse)
library(lubridate)
y <- rnorm(100)
df <- tibble(y) %>%
mutate(os = factor(rep_len(1:5, 100)),
date = seq(from = ymd('2013-01-01'), by = 1, length.out = 100))
ggplot(df, aes(x = date, y = y, colour = os)) +
geom_line() +
geom_vline(
aes(xintercept = min(date), linetype = 'os 1'),
colour = 'red') +
geom_vline(
aes(xintercept = median(date), linetype = 'os 2'),
colour = 'blue') +
geom_hline(
aes(yintercept = 1, linetype = "dashed"),
colour = "black"
) +
scale_linetype_manual(
name = 'lines',
values = c('os 1' = 1, 'os 2' = 1),
guide = guide_legend(override.aes = list(colour = c('red', 'blue'))))
output:
What is wrong with output:
- The
geom_hlineis missing. - The legend combines the vline and the hline to form a cross.
Correct output:
- THe geom_hline should be drawn.
- Need a separate legend for the vlines and hlines. i.e., lines in the vline legend should be vertical while lines in the hline legend should be horizontal.

This could be achieved by
hlinetoscale_linetype_manual