I have a multiple line plot with a segment of each line highlighted the same way as discussed here. Here is the reproducible example:
df <- data.frame(x = 1:100,y1 = rnorm(100,1,100),y2=rnorm(100,5,50),y3=rnorm(100,10,500),y4=rnorm(100,1,200),col1 = c(rep("red", 50), rep("black", 10), rep("red", 40)),col2=c(rep("blue", 50), rep("black", 10), rep("blue", 40)),col3=c(rep("orange", 50), rep("black", 10), rep("orange", 40)),col4=c(rep("cyan", 50), rep("black", 10), rep("cyan", 40)))
ggplot(df, aes(x=x, y=y1)) + geom_line(aes(colour=col1, group=1))+geom_line(aes(x=x, y=y2,col=col2,group=1))+geom_line(aes(x=x, y=y3,col=col3,group=1))+geom_line(aes(x=x, y=y4,colour=col4, group=1))+geom_line(aes(x=x, y=y4,col=col4,group=1))+scale_color_manual(values=c("black","blue","red","orange","cyan"),labels=c("new","s1on","s2off","s2_on","s1_off"),name="")
This gives the order of the items in the legend as shown in the image below. I want to reorder them. Since each line has a segment and each segment has the same color "black", I am not able to use breaks
argument in scale_color_manual
as it requires 5 values and the plot is plotted for 4 columns in the dataframe. How can I reorder the items in the legend in the order s1on, s2on, s1_off, s2_off and new?