R - 因子水平的订单(和ggplot组轴顺序) [英] R - factor level orders (and ggplot group axis order)

查看:193
本文介绍了R - 因子水平的订单(和ggplot组轴顺序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 头(pctCStack,4)

BRK时间x
1 ICs十二月2013 0.6717300
2经理2013年12月0.8024344
3集成电路2014年7月2.0417851
4经理2014年7月1.3047233

水平(pctCStack $ BRK)< - c(0 -2年,2-4年,4年以上,IC,经理)

头(pctCStack,4)
BRK时间x
1 0-2 2013年12月0.6717300
2 2-4 2-4年2013年12月0.8024344
3 0-2 2014年7月2.0417851
4 2-4年2014年7月1.3047233


解决方案

尝试 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

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

解决方案

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

这篇关于R - 因子水平的订单(和ggplot组轴顺序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆