Stack Exchange
How to sort the index legend by number and not by string in R
I have this code:
 gg = ggplot() +  geom_line(data = correctAnswersPerStimu_1010v1, size=1,
                            aes(x=as.character(testPhase), y=percent_correct, group=as.character(individualIndex), colour = as.character(individualIndex)))
  
  
  gg = gg + geom_point(data = correctAnswersPerStimu_1010v1, aes(x=as.character(testPhase), y=percent_correct),
                       shape = 23, size = 3, color="black", stroke = 1)
  
  gg = gg + labs(title=paste("Experiment 1010 stimuli correct answers % per phase", sep = ""),
                 x ="Phase", y = "% Correct", fill="Phase")
  
  gg = gg + guides(color = guide_legend(title = "Index")) + ylim(0, 100)

  gg

Thanks generate this graph:

What do I have to change in the ggplot to show the legend sorted by index (0,1,2,3,... 14,15) and not by string (0,1,10,11,...)?

Try using factor() instead of as.character().

Please go through this links Sorting legend or enter link description here
