R:正确重新排序因子水平 - 避免重复的因子水平? {} GGPLOT2

时间:2016-05-29 22:37:03

标签: r

这些问题与因素中的重复级别相吻合,导致:

Warning message:
In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else paste0(labels,  :
  duplicated levels in factors are deprecated

已被多次解决。但是,我仍然无法弄清楚如何正确转换数据,目的是避免在我的数据中引入重复的级别?

我有一个数据框,想要制作一个情节并改变我的情节中的关卡顺序。我创建了重复级别的地方,我无法重写我的order以不引入它们。请如何正确地重新排序我的因素水平?

df1<-data.frame(year = rep(2002:2005, 5),
                  rate  = sample(30,20),
                  gridcode = rep(1:2, each = 10),
                  distance = rep(c(100,200), 10))

# change order - !!! how to write this correctly?
df1$gridcode <- factor(df1$gridcode, 
                       levels=df1$gridcode[
                       order(df1$gridcode, decreasing = TRUE)])               


# plot values                 
ggplot(df1,aes(x = distance, 
                               y= rate, 
                               fill = as.factor(gridcode)))  +    
  geom_bar(position = "stack", stat = "identity")  +
  facet_grid(. ~ year)

1 个答案:

答案 0 :(得分:2)

您需要在unique规范周围包含levels=,否则您将分配多个时间段的水平:

unique(df1$gridcode)[order(unique(df1$gridcode), decreasing = TRUE)]
#[1] 2 1

VS

df1$gridcode[order(df1$gridcode, decreasing = TRUE)]
#[1] 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1