I am creating a taxanomic bar chart using ggplot but I need to change the layout of the stacked segments. This is my code to create the plot:
ggplot(full_genus_new, aes(x=as.factor(Var2), y=value, fill=Var1)) +
geom_bar(stat="identity", position = "fill", aes(order = -value)) +
scale_x_discrete() +
ylab("Relative Microbial Activity") +
facet_grid(cols=vars(PEDIS), scales = "free_x", space = "free_x") +
theme_classic() +
scale_fill_manual(values = Cb64k) +
scale_y_continuous(labels=percent_format()) +
theme(legend.position = "bottom", legend.text = element_text(size=8)) +
guides(fill=guide_legend(title="Taxa")) +
xlab("Patient") +
theme(legend.key.size = unit(0.5,'cm'))
Which produces the following:
How can I reorder the stacked segments based on size so that the larger segments start at the bottom and decrease in size until the top of the y axis is reached?