R 定义因子时发出警告:不推荐使用因子中的重复级别
我在R区的雷达图有点问题。尽管图很好,但我得到以下警告:R 定义因子时发出警告:不推荐使用因子中的重复级别,r,ggplot2,ggproto,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 messa
> 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
这是对应雷达图的代码(可能只有我定义数据集的第一部分是相关的,但是的……这就是我丢失的地方):
colnames(数据集)=c(“类型”、“变量”、“值”)
数据集$value=as.numeric(数据集$value)
dataset$variable是的,几乎所有这些都与您的问题无关
您正在尝试创建具有以下级别的因子:rev(数据集$variable)
。这将产生:
[1] C3 C3 C3 C3 C2 C2 C2 C2 C1 C1 C1
查看您如何复制级别?你只需要按照你想要的顺序将每个关卡设置一次。默认值为排序(唯一(数据集$variable))
,它给出C1 C2 C3
,或者您可以使用rev(唯一(数据集$variable)
给出C3 C1
forcats
软件包有几个方便的功能,可以轻松地创建或更改因子及其级别顺序。太好了,非常感谢您的回复!行dataset$variable@Jonas,这帮我解决了几个月来一直存在的问题。谢谢。
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] C3 C3 C3 C3 C2 C2 C2 C2 C1 C1 C1