I have a dataframe which has a set of manufacturers and collected data for those manufacturers. The list of manufacturers and/or the attribute data can change, depending on the run. I display this as a line chart in ggplot, but what I want is to have the legend order match the 'up/down' order of the final year of data. So for this chart: Default Legend Order
I'd like to see the legend order (and color) be Yoyodyne (purple), Widget (green), Wonka (blue) and Acme (red). I can't (or don't think I can) use scale_color_manual as the data-in from one model run to the next the end-order (in 2032) may differ and/or the list of manufacturers may differ.
Code for doing chart is (last part, pz, just to simplify x axis display):
px <- ggplot(bym, aes(x=Model.Year, y=AverageCost, colour=Manufacturer))
py <- px + ggtitle("MyChart") + labs(x="Year", y="Foo") + geom_line(size=0.5) + geom_point()
pz <- py + scale_x_continuous(breaks=c(min(bym$Model.Year),max(bym$Model.Year)))
pz