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.