Reorder barplot in ggplot

Hi i want to reorder barplot according to class.

This ggplot code does not appear in the order of classes when displayed as a plot.

Can you please let me know what I am missing?

families <- tibble::tribble(
~supp, ~class, ~dose, ~len, ~sd,
"CON", "Firmicutes", "Lachnospiraceae", 4.97, 1.73,
"CON", "Bacteroidota", "Bacteroidales RF16 group", 1.48, 0.51,
"CON", "Verrucomicrobiota", "WCHB1-41", 0.65, 0.43,
"CON", "Firmicutes", "Hungateiclostridiaceae", 0.6, 0.28,
"CON", "Synergistota", "Synergistaceae", 0.5, 0.45,
"CON", "Firmicutes", "Defluviitaleaceae", 0.08, 0.07,
"CON", "Cyanobacteria", "Gastranaerophilales", 0.04, 0.04,
"CON", "Firmicutes", "Butyricicoccaceae", 0.02, 0.01,
"CON", "Firmicutes", "Erysipelotrichaceae", 0.02, 0.02,
"CON", "Thermoplasmatota", "Methanomethylophilaceae", 0.01, 0.01,
"CON", "Verrucomicrobiota", "Victivallaceae", 0, 0.01,
"AUP", "Firmicutes", "Lachnospiraceae", 3.65, 1.16,
"AUP", "Bacteroidota", "Bacteroidales RF16 group", 2.26, 0.97,
"AUP", "Verrucomicrobiota", "WCHB1-41", 1.78, 1.12,
"AUP", "Synergistota", "Synergistaceae", 1.22, 0.93,
"AUP", "Firmicutes", "Hungateiclostridiaceae", 0.33, 0.18,
"AUP", "Cyanobacteria", "Gastranaerophilales", 0.15, 0.18,
"AUP", "Firmicutes", "Defluviitaleaceae", 0.04, 0.04,
"AUP", "Verrucomicrobiota", "Victivallaceae", 0.04, 0.05,
"AUP", "Thermoplasmatota", "Methanomethylophilaceae", 0.03, 0.01,
"AUP", "Firmicutes", "Butyricicoccaceae", 0.01, 0.01,
"AUP", "Firmicutes", "Erysipelotrichaceae", 0.01, 0.01
)
head(families)

Created on 2022-06-23 by the reprex package (v2.0.1)


library(tidyverse)
library(readxl)
library(ggplot2)

families <- read_csv(file = "raw_data/taxa/families.csv")

p<- ggplot(families, aes(x=class, y=len, fill=supp)) +
geom_bar(stat="identity", color="black",
position=position_dodge()) +
geom_errorbar(aes(ymin=len-sd, ymax=len+sd), width=0.2,
position=position_dodge(.9)) +
print(p)

p+labs( y = "Relative abundance (%)")+
theme_classic() +
scale_fill_manual(values=c('Black','Red')) +
coord_flip()

Is this what you mean?

library(tidyverse)

families <- tibble::tribble(
    ~supp, ~class, ~dose, ~len, ~sd,
    "CON", "Firmicutes", "Lachnospiraceae", 4.97, 1.73,
    "CON", "Bacteroidota", "Bacteroidales RF16 group", 1.48, 0.51,
    "CON", "Verrucomicrobiota", "WCHB1-41", 0.65, 0.43,
    "CON", "Firmicutes", "Hungateiclostridiaceae", 0.6, 0.28,
    "CON", "Synergistota", "Synergistaceae", 0.5, 0.45,
    "CON", "Firmicutes", "Defluviitaleaceae", 0.08, 0.07,
    "CON", "Cyanobacteria", "Gastranaerophilales", 0.04, 0.04,
    "CON", "Firmicutes", "Butyricicoccaceae", 0.02, 0.01,
    "CON", "Firmicutes", "Erysipelotrichaceae", 0.02, 0.02,
    "CON", "Thermoplasmatota", "Methanomethylophilaceae", 0.01, 0.01,
    "CON", "Verrucomicrobiota", "Victivallaceae", 0, 0.01,
    "AUP", "Firmicutes", "Lachnospiraceae", 3.65, 1.16,
    "AUP", "Bacteroidota", "Bacteroidales RF16 group", 2.26, 0.97,
    "AUP", "Verrucomicrobiota", "WCHB1-41", 1.78, 1.12,
    "AUP", "Synergistota", "Synergistaceae", 1.22, 0.93,
    "AUP", "Firmicutes", "Hungateiclostridiaceae", 0.33, 0.18,
    "AUP", "Cyanobacteria", "Gastranaerophilales", 0.15, 0.18,
    "AUP", "Firmicutes", "Defluviitaleaceae", 0.04, 0.04,
    "AUP", "Verrucomicrobiota", "Victivallaceae", 0.04, 0.05,
    "AUP", "Thermoplasmatota", "Methanomethylophilaceae", 0.03, 0.01,
    "AUP", "Firmicutes", "Butyricicoccaceae", 0.01, 0.01,
    "AUP", "Firmicutes", "Erysipelotrichaceae", 0.01, 0.01
)

ggplot(families, aes(y = fct_inorder(class), x = len, fill = supp)) +
    geom_col(color = "black",
             position = position_dodge()) +
    geom_errorbar(aes(xmin = len - sd, xmax = len + sd),
                  width = 0.2,
                  position = position_dodge(.9)) +
    labs( y = "Relative abundance (%)")+
    theme_classic() +
    scale_fill_manual(values = c('Black', 'Red'))

Created on 2022-06-22 by the reprex package (v2.0.1)

If not, what should be the order?

Thank you for your help andresrcs.

I am sorry that i did not explain properly.

I want to make this plot like this. For example,

The y-axis is made by family, but I want to sort by the phylum that corresponds to the family.

families %>% 
    arrange(class, len) %>% 
    ggplot(aes(y = fct_inorder(dose), x = len, fill = supp)) +
    geom_col(color = "black",
             position = position_dodge()) +
    geom_errorbar(aes(xmin = len - sd, xmax = len + sd),
                  width = 0.2,
                  position = position_dodge(.9)) +
    labs( y = "Relative abundance (%)")+
    theme_classic() +
    scale_fill_manual(values = c('Black', 'Red'))

Created on 2022-06-22 by the reprex package (v2.0.1)

1 Like

Thank you very much!

This is exactly what i looking for!

Have safe!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.