I want to make a line plot with custom colors and additional vlines with custom colors too.
It all works well except I cannot find a way of removing the vline groups from the legend... after checking many questions, all I could get is show.legend=FALSE but that does not the trick completely.
This is my MWE:
datn <- read.table(header=TRUE, text='
supp dose length
OJ 0.5 13.23
OJ 1.0 22.70
OJ 2.0 26.06
VC 0.5 7.98
VC 1.0 16.77
VC 2.0 26.14
')
datn$supp <- factor(datn$supp, levels=unique(datn$supp))
plot_palette <- c("indianred1","darkgoldenrod1")
vlines <- data.frame(xint = c(1,1.25,1.75), grp = LETTERS[1:3], clr = c("lightskyblue1","cadetblue2","cyan4"))
plot_palette <- c(plot_palette, vlines$clr)
#
P <- ggplot2::ggplot(datn, ggplot2::aes(x=dose, y=length, color=supp)) +
ggplot2::geom_line(linetype = 1, linewidth = 1.75) +
ggplot2::geom_vline(data=vlines, ggplot2::aes(xintercept=xint, color=grp), linetype=4, linewidth=2.25, show.legend = FALSE) +
ggplot2::scale_color_manual(values=plot_palette) +
ggplot2::theme_light()
P
grDevices::dev.off()
which produces:
For clarity, all I want to do is to remove groups A, B, C from the legend.
I do not even understand why they show there in the first place, since they are not supp levels and I explicitly specify show.legend=FALSE for geom_vline... anyone can explain?

You could use the
breaks=argument to set the values which should be displayed in the legend. However, to still color all lines you have to set thelimits=or use a named vector for the color palette passed tovalues=as I do below: