0
votes

I am trying to plot the performance of individuals on a number of tasks. The performance is rated in categories, and I would like to show each individual's overall performance as a stacked bar chart where Y represents the percentage of answers in each performance category, with positive values for good and negative values for bad performance or missing answers. Here's a toy dataset and the current plot I've managed to produce:

df <- data.frame(SRC = rep(LETTERS[1:14],each=6),
                 CAT = rep(c("Excellent","VeryGood","Good","Poor","Failing","Missing"),times=14),
                 PERCENT = c(29.3, 23.3, 30, -13.3, -4, 0, 16.7, 15.3, 38.7, -14.7, -4.7, 
                             -10, 12, 9.3, 30.7, -19.3, -19.3, -9.3, 2.7, 6.7, 20, -23.3, 
                             -14, -33.3, 16, 23.3, 20.7, -10.7, -9.3, -20, 24.7, 22, 12.7, 
                             -8, -2, -30.7, 14, 15.3, 23.3, -4, -4.7, -38.7, 4.7, 6, 60, -24, 
                             -4.7, -0.7, 8, 13.3, 57.3, -16, -3.3, -2, 8, 11.3, 62, -12.7, 
                             -5.3, -0.7, 9.3, 14.7, 64.7, -10, -1.3, 0, 20.1, 20.9, 32.5, 
                             -1.5, 0, 0, 14.2, 10.4, 33.2, -6.6, -2.8, 0, 14.7, 18.7, 55.3, 
                             -10.7, 0, -0.7))

df$CAT <- ordered(df$CAT,levels=c("Excellent","VeryGood","Good","Poor","Failing","Missing"))

ggplot(df, aes(x=SRC, y=PERCENT, fill=CAT,group=CAT,group=SRC)) + 
  geom_bar(position="stack", stat="identity")

This is the figure:

enter image description here

It's almost what I want, except CAT is ordered reversely for negative values and I want the bars to stack according to the factor levels and the fill legend for negative values as well, i.e. Poor>Failing>Missing. This surely came up before, but I couldn't find a solution here or elsewhere. Thanks in advance!

1

1 Answers

1
votes

enter image description hereusing forecats library:

library(forecats)



df %>% 
mutate(CAT=fct_relevel(CAT,"Excellent","VeryGood","Good","Missing","Failing","Poor")) %>% 
ggplot( aes(x=SRC, y=PERCENT, fill=CAT)) + 
  geom_bar(position="stack", stat="identity")