问题
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