lll
lll

Reputation: 1109

R: how to customize the legend labeling of ggplot2

I have plot like the following using the ggplot2 (code attached), but I am wondering how I can change the legend into the proper order - having sp (spring) comes before au (autumn). I want the legend order to be like: 2008 spring, 2008 autumn, 2009 spring, 2009 autumn,...etc. But what I currently have is 2008au, 2008sp, ...etc. I have tried adding scale_fill_discrete, but it is not working.

ggplot(derivative_noHS, aes(x=Date, y=derv, linetype = Season)) + 
geom_line() +
 scale_linetype_manual(values = c("dashed","solid","dashed","solid","dashed","solid","dashed",
                                 "solid","dashed","solid","dashed","solid","dashed","solid"))+
facet_wrap(~Market, scales="free_y")+ geom_hline(aes(yintercept = positive.cut), derivative_noHS)+
 geom_hline(aes(yintercept = zero), 
 derivative_noHS)+
 geom_hline(aes(yintercept = negative.cut), derivative_noHS) + ggtitle("Figure   2 - Derivatives of Market Indices")+
    scale_fill_discrete(breaks=c("2008sp","2008au","2009sp","2009au","2010sp","2010au","2011sp",
                           "2011au","2012sp","2012au","2013sp","2013au","2014sp","2014au"),
                  labels = c("2008sp","2008au","2009sp","2009au","2010sp","2010au","2011sp",
                             "2011au","2012sp","2012au","2013sp","2013au","2014sp","2014au")) +
ylab("Derivative Values")

enter image description here

Upvotes: 1

Views: 56

Answers (1)

loki
loki

Reputation: 10350

You can try ordered before calling ggplot()

derivative_noHS$Season <- ordered(derivative_noHS$Season, c("2008sp", "2008au", "2009sp", 
                                                          "2009au", "2010sp", "2010au", 
                                                          "2011sp", "2011au", "2012sp", 
                                                          "2012au", "2013sp", "2013au", 
                                                          "2014sp", "2014au"))

Upvotes: 1

Related Questions