I am trying to add a manual legend to a plot without modifying the dataset, because the dataset and lines that mark mean, median etc. are different concepts.
Approaches to solve the problem modifying the data exist, e.g. Adding manual legend to a ggplot
Here is a simplified example, the real dataset is more complex:
vec <-c(rep(1,100),rep(2,100),rep(3,80),rep(4,70),rep(5,60))
tbl <- as_tibble(vec)
mean_vec <- mean(vec)
cols <- c("Frequency"="grey",
"mean"="blue")
ggplot(as_tibble(tbl)) +
aes(x = value) +
geom_histogram(binwidth=1) +
geom_vline(xintercept=mean_vec,col="blue",size=1)+
theme_minimal() +
scale_color_manual(values=cols)+
scale_fill_manual(name="Test",values=cols)
Why is the legend missing in the plot?