I was trying to order the bars in descending order (all the segments together) using reorder(). However, this seems not to work. How can I do this?
library(tidyverse)
# Sample data frame
df <- data.frame(var = sample(paste0("x", 1:10), size = 100, replace = TRUE),
var2 = sample(c("A", "B", "C", "D"), size = 100, replace = TRUE))
# Stacked barplot
df %>% group_by(var) %>%
count(var2) %>%
ggplot(aes(x = reorder(var, n, fun = "sum"), y = n, fill = var2)) +
geom_col()