Home > Net >  Order x-axis categorical variable using another categorical variable
Order x-axis categorical variable using another categorical variable

Time:06-01

I have a dataframe like follows:

Class       ID   Stage   Abundance          Substrat
OTUA      rep1  A-X1    123                  G
OTUA      rep2  A-X1    234                  PC
OTUA      rep3  A-X1    [numerical values]   [only two categorical values]
OTUB      rep1  A-X1
OTUA      rep1  EGG
OTUA      rep2  EGG
OTUA      rep1  EL
...       ...   ...    ...

I want to plot in geom_bar the ID of each sample by its stacked abundance for each different variable in the Class.

graph <- ggplot(data, aes(x=ID, y=Abundance, fill=Class))  
  facet_grid(~substrat, scales="free_x")  
  geom_bar(aes(color=Class, fill=Class), stat="identity", position="stack")

I get something like this:

enter image description here

this works well, but I want to order the x axis data (ID) by the stage variable, with the specific order : "A-X0", "EGG", "EL", "LL","PP","P","A-X1", but I do not want to have the stage as label, I want to still have the ID as labels.

I have tried:

graph$stage <- as.character(graph$stage)
graph$stage <- factor(x = graph$stage, levels = c("A-X0", "EGG", "EL", "LL","PP","P","A-X1"))

and:

test <- ggplot(data_ps.sam.env.rel, aes(x=reorder(ID, stage), y=Abundance, fill=Class))  
  facet_grid(~substrat, scales="free_x")  
  geom_bar(aes(color=Class, fill=Class), stat="identity", position="stack") 

but i still get the samples in a random order, instead I expect that all the ID with "J8" would be together, all those with "J21" together, etc

CodePudding user response:

It could have been greate to have an example of your data using dput. What do you think about adding a new variable using paste0(d$stage, d$ID) using this new variable in ggplot, and then removing the d$stage in the label using regex?

CodePudding user response:

I resolved this issue if anyone is interested, after doing the graph, I specified the order of the factors:

graph$data$ID <- as.character(ps.1.sample_class$data$ID)
graph$data$ID <- factor(x = graph$data$ID, 
                        levels = unique(graph$data$ID[order(as.character(graph$data$stage))])) 
  • Related