R - factor level orders (and ggplot group axis order)

大憨熊 提交于 2019-12-12 04:05:20

问题


I am trying to control the order of discrete groups on an axis in ggplot, so I am setting the order of my factor variable. Using the levels() function actually seems to change the data in the data frame - not the way R treats the underlying ordering of the data (see below). Am I doing something wrong? which function should I use instead of levels()? thanks!

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:


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

Here's a reference: http://www.stat.berkeley.edu/~s133/factors.html



来源:https://stackoverflow.com/questions/25167858/r-factor-level-orders-and-ggplot-group-axis-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!