I have a dataframe containing numerical (percentages) and categorical variables. I'd like to produce a stacked barplot (using ggplot2) with the colums (categorical variables) sorted by the numerical variable.
I tried this:
How to control ordering of stacked bar chart using identity on ggplot2
and this:
https://community.rstudio.com/t/a-tidy-way-to-order-stacked-bar-chart-by-fill-subset/5134
but I am not familiar with factors and I'd like to understand more.
# Reproduce a dummy dataset
perc <- c(11.89, 88.11, 2.56, 97.44, 5.96, 94.04, 6.74, 93.26)
names <- c('A', 'A', 'B', 'B', 'C', 'C', 'D', 'D')
df <- data.frame(class = rep(c(-1, 1), 4),
percentage = perc,
name = names)
# Plot
ggplot(df, aes(x = factor(name), y = percentage, fill = factor(class))) +
geom_bar(stat = "identity") +
scale_fill_discrete(name = "Class") +
xlab('Names')
This code produces a plot whose bars are ordered by the variable "names". I'd like to order it by the variable "percentage". Even if I manually order the dataframe, the resulting plot is the same.