Order graph x axis barplot

How do I change the order of elements on the X axis?92582136_212244263415130_8238397201064132608_n

Right now the order is as shown on the picture. The order should be: Control, YUN_S1.0, AMB_F471, AMB_V001, AMB_V038, HAN016, HNP007, CON21

The data are ordered as they should, but Rstudio puts them in alfabetical order.

This is the current code:

add_el <- theme_bw() +
theme(axis.text.y =element_text(size=16),
axis.text.x = element_text(size=16, angle = 75, hjust = 1),
axis.title=element_text(size=16),
legend.title=element_blank(),
legend.text = element_text(size = 14),
legend.position="bottom",
strip.background = element_rect(colour = "black", fill = "white"),
strip.text.x = element_text(size=18, face="bold"),
strip.text.y = element_text(size=16))

ggplot(FC, aes(x = Probiotics, y = Concentration, fill= Species)) +
geom_bar(stat="identity", color="black",
position=position_dodge()) +

Thanks!
geom_errorbar(aes(
ymin = Concentration-SD,
ymax = Concentration+SD),
width=.2,
position=position_dodge(.9)) +
geom_col() +
labs(x = "Conditions", y= "Concentration (log(10) cells/mL)") +
add_el

I have tried:
FC <- read.xlsx(xlsxFile = "YUNexperiment1.xlsx", sheet = 1, skipEmptyRows = TRUE)
FC <- FC %>% mutate(Probiotics=factor(Probiotics, levels=c("Control", "YUN_S1.0", "AMB_F471",
"AMB_V001", "AMB_V038", "HAN016", "HNP007", "CON21"), ordered=TRUE))

Setting the factor levels should work. Try the following example.

library(ggplot2)
library(dplyr)
FC <- data.frame(Probiotics = c("Control", "YUN_S1.0", "AMB_F471",
                                "AMB_V001", "AMB_V038", "HAN016", 
                                "HNP007", "CON21"), 
                  Concentration =  c(2, 2.4, 3, 2.6, 2.4, 3.2, 2.2, 3))
ggplot(FC, aes(x = Probiotics, y = Concentration)) + geom_col()

FC <- FC %>% mutate(Probiotics=factor(Probiotics, 
                                      levels=c("Control", "YUN_S1.0", "AMB_F471",
                                               "AMB_V001", "AMB_V038", "HAN016", 
                                               "HNP007", "CON21"), ordered=TRUE))
ggplot(FC, aes(x = Probiotics, y = Concentration)) + geom_col()

Created on 2020-04-11 by the reprex package (v0.3.0)

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

Thank you! It works now!