定义因子时发出警告:不推荐使用因子中的重复级别

Jon*_*nas 16 r ggplot2 ggproto

我在R中的雷达图表有点麻烦.即使情节很好,我也收到以下警告:

> 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
Run Code Online (Sandbox Code Playgroud)

我在其他帖子中看到了同样的错误,但我真的不明白如何将答案应用到我的数据集......

这是我的数据集

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
Run Code Online (Sandbox Code Playgroud)

这是相应雷达图的代码(可能只是我定义我的数据集的第一部分是相关的,但是......这就是我丢失的地方):

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() 
Run Code Online (Sandbox Code Playgroud)

Axe*_*man 27

是的,几乎所有这些都与您的问题无关.

您正在尝试创建具有以下级别的因子:rev(dataset$variable).产量:

[1] C3 C3 C3 C3 C2 C2 C2 C2 C1 C1 C1
Run Code Online (Sandbox Code Playgroud)

看看你如何复制水平?您希望按照您想要的顺序只为每个级别设置一次.默认值是sort(unique(dataset$variable)),C1 C2 C3或者你可以rev(unique(dataset$variable)用来给出C3 C2 C1.

forcats软件包具有多种便利功能,可轻松制作或更改因素及其级别的顺序.

  • 太好了,非常感谢你的回复!行`dataset $ variable < - factor(dataset $ variable,levels = rev(unique(dataset $ variable)),ordered = TRUE)` (2认同)
  • @乔纳斯,这帮助我解决了我几个月来一直遇到的问题。谢谢。 (2认同)