定义因子时出现警告:已弃用因子中的重复级别 [英] Warning when defining factor: duplicated levels in factors are deprecated

查看:559
本文介绍了定义因子时出现警告:已弃用因子中的重复级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



 <$ c我的雷达图在R中遇到了一些麻烦。即使情节没有问题,我仍然收到以下警告: $ C>> source('〜/ .active-rstudio-document')
警告信息:
在`levels< -`(`* tmp *`,value = if(nl == nL)as.character标签)else paste0(标签,:
重复的因子级别已被弃用
>雷达
警告信息:
1:在levels <-`(`* tmp *` ,value = if(nl == nL)as.character(labels)else paste0(labels,:
因素中的重复级别被弃用
2:在级别< -`(`* tmp *` ,value = if(nl == nL)as.character(labels)else paste0(labels,:
因素中的重复级别被弃用

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



这是我的数据集

  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

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

 <$ c $ (数据集$值)

数据集$变量< - 数据集$ variable,levels = rev(dataset $ variable),ordered = TRUE)

#雷达功能------------------- -----------------------------------------
coord_radar< - 函数(theta =x,start = 0,direction = 1){
theta < - match.arg(theta,c(x,y))
r < - if theta ==x)
y
elsex
ggproto(CordRadar,CoordPolar,theta = theta,r = r,start = start,
方向=符号(方向),
is_linear =函数(coord)TRUE)
}


#雷达图----------- -------------------------------------------------
雷达< - ggplot(数据集,aes(x =变量,y =值,组=类型))+
geom_poly gon(aes(group = type,color = type,fill = type),size = 1,alpha = 0.1)+
scale_fill_manual(values = cbPalette)+
geom_line(aes(group = type,color = $)
scale_colour_manual(values = cbPalette)+
coord_radar()


解决方案

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



您试图创建一个具有以下级别的因子: rev(数据集$变量)。这就产生了:

$ p code $ c> C3 C3 C3 C3 C2 C2 C2 C2 C1 C1 C1

看看您是如何复制关卡的?你只需要按照你想要的顺序每个级别一次。默认是 sort(unique(dataset $ variable)),它给出 C1 C2 C3 ,或者你可以使用 rev(unique(dataset $ variable)给予 C3 C2 C1



forcats 包有几个便利功能,可以轻松制作或更改因素及其级别的顺序。


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() 

解决方案

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.

这篇关于定义因子时出现警告:已弃用因子中的重复级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆