在rCharts dPlot中重新排序因子

时间:2014-10-31 07:14:42

标签: r rcharts

如何对dPlot中的因素进行重新排序,因为"例外"位于最左侧,其次是" Fine"," Mediocre","麻烦","危险"和"临界"

数据集:

dat<-structure(list(MainDept = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 
1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 
5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L), .Label = c("Business Development", 
"Finance Department", "HR & Admin Department", "Manufacturing & Engineering Department", 
"QA Department", "Supply Chain Department"), class = "factor"), 
    Level = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
    2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 
    5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L), .Label = c("Critical", 
    "Danger", "Exceptional", "Fine", "Mediocre", "Trouble"), class = "factor"), 
    Freq = c(0L, 0L, 0L, 29L, 0L, 0L, 0L, 0L, 0L, 108L, 0L, 0L, 
    0L, 0L, 2L, 7L, 12L, 15L, 0L, 7L, 15L, 16L, 73L, 59L, 12L, 
    0L, 0L, 191L, 0L, 0L, 11L, 0L, 0L, 128L, 0L, 0L)), .Names = c("MainDept", 
"Level", "Freq"), row.names = c(NA, -36L), class = "data.frame")

生成情节的代码:

require(rCharts)
plot<-dPlot(y="MainDept", x="Freq",data=dat,groups="Level",type="bar",width=1200)
plot$yAxis(type="addCategoryAxis")
plot$xAxis(type="addPctAxis")
plot$legend(
  x = 0,
  y = 0,
  horizontalAlign = "right"
)

我尝试过:

dat$Level<-as.character(dat$Level)
dat$Level[dat$Level=="Exceptional"]<-1
dat$Level[dat$Level=="Fine"]<-2
dat$Level[dat$Level=="Mediocre"]<-3
dat$Level[dat$Level=="Trouble"]<-4
dat$Level[dat$Level=="Danger"]<-5
dat$Level[dat$Level=="Critical"]<-6
dat$Level<-as.factor(dat$Level)

谢谢。

1 个答案:

答案 0 :(得分:0)

我不了解dPlot,但如果它依赖于分组变量中的级别顺序,那么您应该尝试

dat$Level <- factor(dat$Level, levels = c("Exceptional", "Fine", "Mediocre", 
                                          "Trouble", "Danger", "Critical"))

由于我不知道哪个库dPlot可以找到,这当然是未经测试的。因此,为了将来参考,包含必要库的最小工作示例将有所帮助。