R - 因子级别订单(和ggplot组轴顺序)

时间:2014-08-06 18:45:40

标签: r ggplot2 axis levels factors

我试图控制ggplot中轴上离散组的顺序,所以我设置了我的因子变量的顺序。使用levels()函数实际上似乎改变了数据框中的数据 - 而不是R处理数据的基础排序的方式(见下文)。难道我做错了什么?我应该使用哪个函数而不是levels()?谢谢!

head(pctCStack,4)

       BRK          Time         x
1      ICs December 2013 0.6717300
2 Managers December 2013 0.8024344
3      ICs     July 2014 2.0417851
4 Managers     July 2014 1.3047233

levels(pctCStack$BRK) <- c("0-2 Years", "2-4 Years", "4+ Years", "ICs", "Managers")

head(pctCStack,4)
        BRK          Time         x
1 0-2 Years December 2013 0.6717300
2 2-4 Years December 2013 0.8024344
3 0-2 Years     July 2014 2.0417851
4 2-4 Years     July 2014 1.3047233

1 个答案:

答案 0 :(得分:0)

尝试pctCStack$BRK <- factor(pctCStack$BRK, levels = c("0-2 Years", "2-4 Years", "4+ Years", "ICs", "Managers"))

以下是参考资料:http://www.stat.berkeley.edu/~s133/factors.html