1

I created a stacked bar chart using ggplot and the following code:

ggplot(group, aes(x = variable, y = value, fill = Taxa)) +
  geom_bar(position = "fill", stat = "identity") +
  scale_fill_manual(values = Cb64k) +
  scale_y_continuous(labels = percent_format()) +
  theme(legend.position = "bottom", text=element_text(size=10.5),
        axis.text.x = element_text(angle=0, vjust=1)) +
  guides(fill = guide_legend(ncol=6)) +
  facet_grid(cols=vars(group), scales = "free_x", space = "free_x") +
  ggtitle(opt$gtitle) +
  xlab("Patient ID") + ylab("Relative Activity")

To get this output: Image

Is there a way to reorder the "stacks" in each bar so that the size of the stacks go from largest to smallest starting at the bottom? As you can see with the current output it seems to be random.

mrad
  • 173
  • 1
  • 11
  • 2
    It is difficult to help with a reproducible example. Have you tried this answer? https://stackoverflow.com/questions/33538059/how-to-change-stacking-order-in-stacked-bar-chart-in-r – Ronak Shah Sep 24 '20 at 02:04
  • The order should correspond with the order in your input data. But you can adjust the order with `reorder(variable,value)`. As soon as the ordering is split over multiple facets, this approach won't work. In this case you could try `tidytext::reorder_within` – sambold Sep 24 '20 at 08:08

0 Answers0