I'm creating a simple point and line plot in ggplot similar to the example below:
dat <- iris %>% # dummy data
select(Sepal.Length) %>%
mutate(Type = "Sepal.Length")
ggplot() +
geom_point(data = dat, aes(x = as.numeric(row.names(dat)), y = Sepal.Length, shape = Type), colour = "orange") +
scale_shape_manual(values = 10) +
geom_hline(aes(yintercept = 6, linetype = 'C Red line'), colour = "red", size = 0.5) +
geom_hline(aes(yintercept = 5, linetype = 'A Blue line'), colour = "darkblue", size = 0.5) +
geom_hline(aes(yintercept = 7, linetype = 'B Green line'), colour = "forestgreen", size = 0.5) +
scale_linetype_manual(values = c('solid', 'dashed', 'dotted')) +
labs(linetype = "Line legend") +
labs(shape = "Point legend")
I'ev worked out that the alphabetical order of the 'names' associated with each line controls the order in the legend and I can match a desired line style to those lines using scale_linetype_manual. However, I cannot work out how to get the colours of the lines on the plot to be matched by the colours in linetype legend, which just uses the last specifed line colour?
clr <- c('orange', 'red', 'darkblue', 'forestgreen'); plot(dat$Sepal.Length, pch=20, col=clr[1]); abline(h=c(6, 5, 7), lty=c(3, 1, 2), col=clr[-1]); legend('topleft', legend=c('Sepal.Length', 'Blue', 'Green', 'Red'), pch=c(20, rep(NA, 3)), col=clr, lty=c(NA, c(3, 1, 2)))
, servus!