Hannah H.
Hannah H.

Reputation: 276

Set order of variables in grouped barplot

My aim is to have a grouped barplot with every cluster number as a different bar. This is working for me. However, I don't want R to reorder my variables. I included the approach which was working for me. But it doesn't anymore (I don't know why). I now also get the error message:

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

I found an approach here to reorder the data. However, you need to include every variable manually. I want to use an automated approach, as I have to do the plot with different variables.

This is also why the approach here is not really working for me. Also, when I try to set the ordering in aes with type = variable and then add geom_col(aes(fill = variable)) the order doesn't change and the color of the bars is set to grey.

Here is my reproducible example:

library(ggplot2)

##Create dataset
first <- c("female", "male", "married", "divorced", "female", "male", "married", "divorced")
second <- c(1, 1, 1, 1, 2, 2, 2, 2)
third <- c(54, 46, 30, 70, 70, 30, 20, 80)

df <- data.frame(first, second, third)
names(df) <- c("variable", "cluster", "quantity")

##Attempt to sort the variables -> does not seem to do anything
df$variable <- factor(df$variable, levels = df$variable)

##Set colors for barplot
colors.barplot <- c("#708090", "#D53E4F", "#FBB869", "#F0E442")

##barplot of the results
ggplot(df, aes(y = quantity, x = cluster, fill = variable)) +
  geom_bar( stat = "identity", colour = "white") +
  scale_fill_manual(values = colors.barplot)# 

When I do it like above the bars are stacked in alphabetical order.

Upvotes: 0

Views: 208

Answers (2)

YBS
YBS

Reputation: 21297

You just need unique in your levels

df$variable <- factor(df$variable, levels = unique(df$variable))
df$cluster <- factor(df$cluster, levels = unique(df$cluster))

and you will get

output

Upvotes: 2

Paratept
Paratept

Reputation: 31

Replace your code

df$variable <- factor(df$variable, levels = df$variable)

with

library(forcats)
df$variable <- fct_inorder(df$variable, ordered = NA)

fct_inorder orders the factors in the order that they appear in the data.

Upvotes: 1

Related Questions