使用分组变量按顺序绘制因子

时间:2018-05-11 00:56:49

标签: r

我正在使用R.我有一个如下所示的数据框:

df <- (structure(list(year = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 
       2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 
       5L, 5L), .Label = c("2013", "2014", "2015", "2016", "2017"), 
       class = "factor"), user = structure(c(2L, 4L, 1L, 3L, 5L, 2L, 4L, 1L,
       3L, 5L, 2L, 4L, 1L, 3L, 5L, 2L, 4L, 1L, 3L, 5L, 2L, 4L, 1L, 3L, 5L),
       .Label = c("John", "Laura", "Liz", "Mark", "Martha"), class = "factor"), 
        spent = c(56, 64, 69, 38, 93, 70, 29, 94, 56, 76, 48, 17, 
        74, 67, 100, 29, 16, 23, 10, 51, 72, 35, 77, 83, 17)), 
        class = "data.frame", row.names = c(NA, -25L)))

我正在尝试生成一个直方图,其中y轴为“已用完”变量,x轴为“用户”,每年为一个方面。对于每年,应根据“已用完”变量对用户进行订购。

我试过df$user2=factor(df$user, levels = df$user[order(df$year,df$spent)])之类的东西 但我得到一个错误,说第6个因素是重复的。

非常感谢任何帮助!

格里

0 个答案:

没有答案