本文介绍了定义因子时出现警告:已弃用因子中的重复级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 > source('〜/ .active-rstudio-document')警告信息:在`levels< -`(`* tmp *`,value = if(nl == nL)as.character标签)else paste0(标签,:重复的因子级别已被弃用>雷达警告信息: 1:在levels 因素中的重复级别被弃用 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 这是代码相应的雷达图表(可能只有第一部分,我定义我的数据集是相关的,但是...这就是我丢失的地方): 数据集$变量< - 数据集$ variable,levels = rev(dataset $ variable),ordered = TRUE) #雷达功能------------------- ----------------------------------------- coord_radar< - 函数(theta =x,start = 0,direction = 1){ theta r 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> radarWarning messages:1: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else paste0(labels, : duplicated levels in factors are deprecated2: In `levels<-`(`*tmp*`, value = if (nl == nL) as.character(labels) else paste0(labels, : duplicated levels in factors are deprecatedI'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 datasetMSF,C1,2OCA,C1,6SIOA,C1,4CCFF,C1,4MSF,C2,4OCA,C2,2SIOA,C2,6CCFF,C2,2MSF,C3,6OCA,C3,6SIOA,C3,6CCFF,C3,6And 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 C1See 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. 这篇关于定义因子时出现警告:已弃用因子中的重复级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-23 02:05