Home > front end >  How to group variable by quality number?
How to group variable by quality number?

Time:11-18

Here's a madeup dataset that demonstrates the general idea of what I'm working with.

Quality <- sample(1:4, 300, replace = TRUE)
reader_ID <- rep(1:3, each = 100)

df <- data.frame(Quality, reader_ID)
df

quality_percentage <- ggplot(df, aes(x = reader_ID, y = Quality, fill = Quality))  
  geom_bar(position="fill", stat="identity")

quality_percentage

Here is the graph it produced. I'm trying to have each quality grouped together instead of having them all separate.

enter image description here

CodePudding user response:

You can simply sort your data frame by Quality before plotting:

ggplot(df[order(df$Quality),], 
       aes(x = reader_ID, y = Quality, fill = Quality))  
  geom_col(position = "fill")

enter image description here

  • Related