Error "Factor level [2] is duplicated" when not really duplicated

Viewed 7747

I recently upgraded to R Studio Version 1.1.463. However, I am having issues with several ggplots related to factors. Here is one of the cases where I get the error:

I have a data frame (df) with 3 variables:

  • Name (factor, with 3 levels)
  • Type (factor, with 35 levels)
  • Group (factor, with 6 levels)

When I want to arrange a Data Frame (df) by levels of a factor, as follows

df$Type <- factor(df$Type, levels = df$Type[order(df$Group)])

in order to plot a geom_tile as in this example:

print(ggplot(df, aes(x = factor(Name), y = Type, fill = Group)) +
    geom_tile()

I get the following error:

"Error in `levels<-`(`*tmp*`, value = as.character(levels)) : factor level [2] is duplicated"

I have checked the factors for all three variables, using

levels(df$Name)
levels(df$Group)
levels(df$Type)

and they are not duplicated. If I don't run the first line of code mentioned in this example, the plot is produced, but not in the order I want (I want all Types of the same Group to be placed close together).

If I run the graph on R Studio Version 1.0.136, it works fine.

I appreciate any clue as to what could be going on.

0 Answers
Related