I would like to make a bar plot in ggplot2 and have ggplot2 graph my X-axis in the same order that I provide in my Day44$Sample column. Here is an example (my real data has 59 factor levels in the Day44$Sample column.
Day44 <- data.frame(Sample = c(rep(6, 3), rep(8, 5), rep(12, 8), rep(100, 7), rep("41*", 3), rep("198*", 5)),
Phylum = c(rep("Proteobacteria", 3), rep("Actinobacteria", 5), rep("Firmicutes", 8),
rep("Chloroflexi", 7), rep("Cyanobacteria", 3), rep("Bacteroidetes", 5)),
Rel_Abund = c(rep(2.2, 3), rep(0.15, 5), rep(0.047, 8), rep(1.2, 7), rep(0.33, 3), rep(4.5, 5)))
I have read that in order to plot in the same order as my column, I have to 'tell' ggplot2 that I have an ordered factor already - based on this post: Avoid ggplot sorting the x-axis while plotting geom_bar()
Following the post:
Day44$Sample <- factor(Day44$Sample, levels = Day44$Sample)
when I get the following error:
Error in
levels<-
(*tmp*
, value = as.character(levels)) : factor level 2 is duplicated
So, I found this post: Warning when defining factor: duplicated levels in factors are deprecated
and following that lead (omitting the sort
recommended in the post bc I don't need it sorted):
Day44$Sample <- factor(Day44$Sample, levels = unique(Day44$Sample)
which I then graph with:
ggplot() + geom_bar(aes(x = Sample, y = Rel_Abund, fill = Phylum), data = Day44, stat = 'identity')
And it gives me a nice bar plot, however; the x-axis is sorted anyway.
This piece of code clues me into the error, which gives:
factor(unique(Day44$Sample))
6 8 12 100 41* 198*
Levels: 100 12 198* 41* 6 8
How can I change this to where my levels are in the same order as my unique values for Day44$Sample?
I know I can manually put them in, like this:
Day44$Sample <- factor(Day44$Sample, levels = c("6", "8", "12", "100", "41*", "198*"))
producing the exact graph that I want, but this isn't practical, since my real data has 59 levels and I have to do this with multiple other graphs, too. I'll never finish doing this manually.
Any suggestions?