Search code examples
rstackedbarseries

Stacked bar graph in R: re-ordering the data


I've produced a bar graph, however I want the data to be switched. I want the blue area (the live cells) on the bottom and the dead cells on the top. I tried reorder, reordering it by making it a factor but it all didn't work.

Who can help me?

Code I have now:

b_stack1<-ggplot(Stack1, aes(x=Condition, y=N, fill=Type)) + 
  geom_bar(aes(fill = Type), stat="identity", position="stack", color="black") +
  scale_fill_manual(values=c("lightblue","grey")) +
  theme_bw() + theme(legend.position="right", axis.title.x = element_blank())

Graph I have now: Graph


Solution

  •     `install.packages("forcats") 
    b_stack1<-ggplot(Stack1, aes(x=Condition, y=N, fill = forcats::fct_rev(Type))) + 
    

    geom_bar(stat="identity", position="stack", color="black") + scale_fill_manual(values=c("lightblue","grey"))`

    Forcats did the trick