Reorder barplot in ggplot – tidyverse

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()

Read more here: Source link