2
library(tidyverse)

df <- read.table(text = "Name   WT  Mutant
'cellular process'  200 2
'Biological phase'  150 5
'cell process'  100 9", header = TRUE)

df %>%
  pivot_longer(-Name) %>%
  ggplot(aes(x = Name, y = value, fill = name)) +
  geom_col(position = position_stack()) +
  coord_flip() +
  facet_wrap(~name)

Using the above code i am unable to change the group position in the graph.enter image description here

I need WT group first and Mutant group second. Thanks for your help in advance.

I need to change the position of the group as above stated. Many thanks for the help.

1 Answers1

1

One solution is to set levels in name

df %>%
  pivot_longer(-Name) %>%
  mutate(name = factor(name, levels=c("WT", "Mutant"))) %>% 
  ggplot(aes(x = Name, y = value, fill = name)) +
  geom_col(position = position_stack()) +
  coord_flip() +
  facet_wrap(~name)

enter image description here

Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138