How do I force facet_wrap to have my plots appear in the factor order I specify? The code below I had used to work perfectly for this but it seems it no longer works and I can’t understand what has changed.
Code:
bind_rows("Agapostemon" = top10_Agapostemon,
"Bombus" = top10_Bombus,
.id = "BeeGenus") %>%
mutate(Genus = reorder_within(Genus, by = Total, within = BeeGenus)) %>%
mutate(across(BeeGenus, ~factor(., levels=c("Bombus","Agapostemon")))) %>%
ggplot(aes(x=Genus, y=Total, fill=Family)) +
geom_col(colour="black") +
geom_text(aes(label=format(Total, big.mark=",")), hjust=1.1,
color="white", fontface="plain", size=4) +
coord_flip() +
scale_x_reordered() +
scale_y_continuous(trans="log10", limits=c(1,2500000), labels=scales::comma,
expand=expansion(mult=c(0,.05))) +
facet_wrap(vars(factor(BeeGenus)), scales = "free", ncol = 4, nrow = 3) +
theme_bw() +
theme( # note you can put all your arguments in a single theme() call
axis.title.x = element_text(size=14),
axis.text.x = element_text(colour="black", face="plain", size=10),
axis.title.y = element_text(size=14, vjust=2.5),
axis.text.y = element_text(face="italic", colour="black", size=10),
strip.text = element_text(face="italic", size=12),
legend.text = element_text(size=12),
legend.title = element_text(size=14)) +
xlab("Genus") + ylab("nAbundance")
> dput(top10_Agapostemon)
structure(list(Domain = c("Bacteria", "Bacteria", "Bacteria",
"Bacteria", "Bacteria", "Bacteria", "Bacteria", "Bacteria", "Bacteria",
"Bacteria"), Family = c("Lactobacillaceae", "???", "Lactobacillaceae",
"Erwiniaceae", "Acetobacteraceae", "Morganellaceae", "Moraxellaceae",
"Neisseriaceae", "???", "Enterobacteriaceae"), Genus = c("Apilactobacillus",
"???.3", "Latilactobacillus", "Pantoea", "???.2", "Arsenophonus",
"Acinetobacter", "Snodgrassella", "???.1", "???.6"), MKC23190 = c(24916L,
1182L, 0L, 4L, 13L, 0L, 0L, 0L, 0L, 0L), MKC23193 = c(59L, 1706L,
0L, 2632L, 20L, 10L, 0L, 42L, 0L, 0L), MKC23194 = c(40L, 2000L,
0L, 0L, 32L, 27L, 50L, 15L, 12L, 0L), MKC23429 = c(262L, 13861L,
4419L, 23L, 185L, 29L, 8L, 6L, 13L, 13L), TJLS23307 = c(1148L,
0L, 0L, 7L, 0L, 7L, 6L, 0L, 0L, 10L), TJLS23438 = c(6L, 0L, 0L,
2L, 0L, 71L, 0L, 0L, 0L, 0L), Total = c(26431, 18749, 4419, 2668,
250, 144, 64, 63, 25, 23)), row.names = c(1L, 11L, 26L, 9L, 6L,
4L, 17L, 3L, 2L, 15L), class = "data.frame")
> dput(top10_Bombus)
structure(list(Domain = c("Bacteria", "Bacteria", "Bacteria",
"Bacteria", "Bacteria", "Bacteria", "Bacteria", "Bacteria", "Bacteria",
"Bacteria"), Family = c("Neisseriaceae", "Morganellaceae", "Enterococcaceae",
"???", "Orbaceae", "???", "Lactobacillaceae", "Enterobacteriaceae",
"Bifidobacteriaceae", "Lactobacillaceae"), Genus = c("Snodgrassella",
"Arsenophonus", "Enterococcus", "???.1", "Gilliamella", "???.5",
"Lactobacillus", "Enterobacter", "Bombiscardovia", "Fructobacillus"
), MKC113 = c(61195L, 15L, 0L, 14L, 9147L, 0L, 0L, 0L, 804L,
0L), MKC116 = c(11451L, 80L, 29L, 24522L, 6L, 0L, 0L, 0L, 7648L,
0L), MKC191b = c(8030L, 1260L, 0L, 1585L, 5148L, 2L, 0L, 0L,
1154L, 0L), MKC192b = c(11L, 163180L, 0L, 0L, 12L, 0L, 0L, 35L,
0L, 34L), MKC193a = c(24874L, 115L, 0L, 3546L, 17438L, 0L, 0L,
0L, 1105L, 0L), MKC23227 = c(130923L, 20L, 0L, 19L, 12L, 0L,
0L, 0L, 0L, 0L), MKC278 = c(49621L, 308L, 0L, 17422L, 7083L,
0L, 0L, 0L, 147L, 0L), MKC294 = c(0L, 765L, 0L, 6459L, 34L, 684L,
813L, 8L, 103L, 8377L), MKC306 = c(4249L, 95L, 0L, 8303L, 3335L,
15025L, 8467L, 60L, 797L, 64L), MKC359 = c(23L, 64L, 22L, 5892L,
768L, 19034L, 1390L, 0L, 488L, 1163L), MKC361 = c(0L, 16597L,
0L, 612L, 0L, 0L, 0L, 12626L, 0L, 0L), MKC371 = c(11L, 565L,
64L, 43592L, 528L, 24L, 1325L, 0L, 17L, 218L), MKC372 = c(0L,
114L, 166536L, 1384L, 543L, 63L, 32L, 0L, 0L, 0L), MKC389 = c(0L,
7832L, 0L, 493L, 395L, 8593L, 1794L, 42L, 35L, 777L), Total = c(290388,
191010, 166651, 113843, 44449, 43425, 13821, 12771, 12298, 10633
)), row.names = c(3L, 4L, 5L, 2L, 13L, 14L, 18L, 10L, 20L, 21L
), class = "data.frame")
I want Bombus to appear first, before Agapostemon, and the mutate(across) bit of code used to work in ordering my plots the way I want, now it won’t work.