(reposted - previously flagged as duplicate but additional MRE illustrates issue more completely)
I'm trying to order one of the factors in a stacked percentage bar plot by the percentage value of that factor but can't manage to work out how to do it.
e.g. in the following I'd like the categories in x
to be ordered by the percentage value of factor q
.
library(tibble)
library(ggplot2)
df <- tibble(x = rep(LETTERS[1:5], 2), y = c(10,20,5,60,30,90,80,95,40,70), z = c(rep("p", 5), rep("q", 5)))
ggplot(df, aes(x = x, y = y, fill = z)) +
geom_bar(stat = "identity", position = "fill") +
scale_y_continuous(labels=scales::percent)
The solution accepted at question Reorder bars in geom_bar ggplot2 uses reorder()
to change the order of x bars, but does not work for my case:
ggplot(df, aes(x = reorder(x, -y), y = y, fill = z)) +
geom_bar(stat = "identity", position = "fill") +
scale_y_continuous(labels=scales::percent)
Produces exactly the same plot as above.
I think the reason is that the data are in tidy format so there are multiple y values for x, but I can't figure out how to map only a subset of y onto x.