R:如何自定义ggplot2的图例标签

时间:2016-09-11 19:45:09

标签: r ggplot2

我使用ggplot2(附带代码)有如下情节,但我想知道如何将图例更改为正确的顺序 - 让sp(spring)出现在au(秋天)之前。我希望传说顺序如下:2008年春季,2008年秋季,2009年春季,2009年秋季......等。但我现在拥有的是2008au,2008sp,等等。我已经尝试添加scale_fill_discrete,但它无法正常工作。

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

1 个答案:

答案 0 :(得分:1)

您可以在致电ordered

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