I've created a plot using geom_segment and I'd like to add a legend for the different line colours. The only solution I've found suggests modifying the linetype in aes(), but this only works for one segment - when I add a second label to another geom_segment then the linetype becomes dashed and the legend just changes to the new colour.
Here's the code for the plot:
library(ggplot2)
rib_x <- seq(1,10,0.5)
rib_ymin <- seq(3,12,0.5)
rib_ymax <- c(3.0,4.4,5.55,6.55,7.28,8.1,8.6,9.1,9.52,9.98,10.3,10.62,10.98,11.2,11.4,11.52,11.7,11.8,12)
ggplot(data.frame())+
geom_segment(aes(x=1, xend=10, y=12, yend=3),colour="dark red",size=1.5)+
geom_segment(aes(x=1, y=3,xend=10,yend=12),colour="green",size=1.5)+
stat_smooth(aes(x=rib_x,y=rib_ymax),se=FALSE,colour="dark green",size=1.5)+
xlab("Agroecological zone")+
ylab("Productivity")+
geom_segment(aes(x=0, xend = 0, y=2, yend=12), size=1.5, arrow=arrow(length=unit(0.6,"cm")))+
theme_bw()+
annotate("text", label="Arid", x=2, y=1)+
annotate("text", label="Semi-arid", x=5, y=1)+
annotate("text", label="Humid", x=8, y=1)+
theme(axis.title=element_text(size=14),axis.text=element_blank(),legend.title=element_blank(),
axis.ticks=element_blank(),panel.border=element_blank(),
panel.grid.major = element_blank(),panel.grid.minor = element_blank())
I'd like to add a legend for the three line colours, where I can also specify the text used in the legend.
Many thanks!