I am trying to make a likert/diverging barplot in ggplot2. Although I set the order of the levels using the factor levels, the order in the barplot is not correct (I use geom_col), in the legend the order is correct.
Can someone help me? Thanks
Reporducible expample:
library(data.table)
library(ggplot2)
data = data.table::data.table(code = paste0("A", c(1,2,4,5)),
label = c("Very good", "Good", "Not good", "Bad"),
pct = c(9.6, 66, -22.7, -1.7),
group = 1)
data$label <- factor(data$label, levels = unique(data[order(code)]$label))
ggplot(data, aes(x = group, y = pct, group = label, fill = label)) +
geom_col(width = 0.8, position = position_stack()) +
coord_flip()
In the plot the level "bad" comes before "not good", as supposed to the other way around.

