Warning when defining factor: duplicated levels in factors are deprecated

半城伤御伤魂 提交于 2019-11-27 02:44:12

问题


I am having a little trouble with my radar chart in R. Even though the plot is fine I am getting the following warning:

> source('~/.active-rstudio-document')
Warning message:
In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else paste0(labels,  :
  duplicated levels in factors are deprecated
> radar
Warning messages:
1: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else paste0(labels,  :
  duplicated levels in factors are deprecated
2: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else paste0(labels,  :
  duplicated levels in factors are deprecated

I've seen the same error in other posts but I didn't really understand how to apply the answers to my dataset ...

This is my dataset

MSF,C1,2
OCA,C1,6
SIOA,C1,4
CCFF,C1,4
MSF,C2,4
OCA,C2,2
SIOA,C2,6
CCFF,C2,2
MSF,C3,6
OCA,C3,6
SIOA,C3,6
CCFF,C3,6

And this is the code for the corresponding radar chart (probably only the first part where I define my dataset is relevant but yeah ... that's where I am lost):

colnames(dataset) = c("type", "variable", "value")
dataset$value = as.numeric(dataset$value)

dataset$variable <- factor(dataset$variable, levels = rev(dataset$variable), ordered=TRUE)

# Radar function ------------------------------------------------------------
coord_radar <- function (theta = "x", start = 0, direction = 1) {
  theta <- match.arg(theta, c("x", "y"))
  r <- if (theta == "x")
    "y"
  else "x"
  ggproto("CordRadar", CoordPolar, theta = theta, r = r, start = start,
          direction = sign(direction),
          is_linear = function(coord) TRUE)
}


# Radar plot ------------------------------------------------------------
radar <- ggplot(dataset, aes(x = variable, y = value, group=type)) +
  geom_polygon(aes(group = type, color=type,fill=type), size = 1, alpha=0.1) + 
  scale_fill_manual(values=cbPalette) +
  geom_line(aes(group = type, color=type)) + 
  scale_colour_manual(values = cbPalette) + 
  coord_radar() 

回答1:


Yes, almost all of that is irrelevant to your problem.

You are trying to create a factor with the following levels: rev(dataset$variable). That yields:

[1] C3 C3 C3 C3 C2 C2 C2 C2 C1 C1 C1

See how you have replicated levels? You'll want to have each level only once, in the order that you want. The default is sort(unique(dataset$variable)), which gives C1 C2 C3, or you could use rev(unique(dataset$variable) to give C3 C2 C1.

The forcats package has several convenience functions to easily make or change factors and the order of their levels.



来源:https://stackoverflow.com/questions/38931194/warning-when-defining-factor-duplicated-levels-in-factors-are-deprecated

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!