0

I am trying to keep geombar from rearranging the x-axis on my stacked barplot.

Data frame looks like this:

enter image description here

I want to follow that same order for the group column (Female: HIV negative, Male: HIV negative, Female: HIV untreated, Male: HIV untreated, Female: HIV on ART, Male: HIV on ART)

This is my rscript to generate the stacked barplot:

data_long<- gather(data_wide, Genus , abundance, Sextaecvirus:Phi29virus, factor_key = TRUE )

ggplot(data_long, aes(x= Group, y= abundance, fill= Genus)) + geom_bar(stat = "identity") + geom_col(position = "fill")+ scale_y_continuous(labels = scales::percent).

Here is an example of how the stacked barplot x-axis looks like following my code: enter image description here

jan.narv
  • 11
  • 1
  • Try with `forcats::fct_inorder(Group)` which makes Group a factor with the levels in the order as in your data. – stefan May 12 '21 at 16:59
  • @stefan Thank you! That worked! Used the code > data_long$Group<- forcats::fct_inorder(data_long$Group). followed by my ggplot code and it worked. – jan.narv May 12 '21 at 17:42

0 Answers0