I am using the package ggh4x and the following set and code to create a boxplot with a nested relation between two categorical variables.
Data used
set1 <- structure(list(Tx = c("Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed",
"Not Exposed", "Not Exposed", "Exposed", "Exposed", "Exposed", "Exposed", "Exposed",
"Exposed", "Exposed", "Exposed", "Exposed", "Exposed", "Not Exposed", "Not Exposed",
"Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Not Exposed", "Exposed", "Exposed",
"Exposed", "Exposed", "Exposed", "Exposed", "Exposed", "Exposed",
"Exposed", "Exposed"), Species = structure(c(1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L), levels = c("Species1", "Species2"), class = "factor"), Size = c(88.5,
83.3, 59.5, 78, 50.3, 57, 78.2, 59, 85, 59.5, 13.1, 50.1, 55,
60.1, 13.8, 27, 57.1, 53.1, 42, 16, 88.8, 26.2, 62, 108.5, 92.3,
74.4, 77.3, 96, 88.7, 77.8, 50.7, 61.9, 65.1, 63.5, 64, 88.6,
53.8, 82.1, 78.8, 75.6)), row.names = c(NA, -40L), class = c("tbl_df",
"tbl", "data.frame"))
Nested boxplot
library(ggplot2)
library(ggh4x)
ggplot(set1, aes(x=interaction(Tx, Species), y=Size)) +
stat_boxplot(geom="errorbar", width = 0.15) +
geom_boxplot(show.legend=FALSE, outlier.shape = NA, aes(fill = interaction(Tx, Species))) +
geom_jitter(width = 0.1, shape=21, colour="black", fill="grey95", stroke=0.5, size=1) +
guides(x="axis_nested") +
theme_classic() +
theme(axis.title = element_text(face="bold"),
text = element_text(family = "serif", size = 12.5))
Right now, the nested relation is displayed on the x-axis just like I wanted. However, the order of the groups is alphabetically, and I'd like to select it myself (with the "Not Exposed" group before "Exposed").
I tried doing it with weave_factors() instead of interaction(), but then the plot doesn't display the nested relation correctly.
Is there an existing method to selectively reorder the groups ?

In 99.9% of questions related to the (re-)ordering of axes, facets or legends the answer is always the same:
While there is an option to achieve your desired result using
weave_factors, it depends on the order of the data (and some additional changes, see below), and hence I think the more robust approach to makeNot Exposedthe first category is to useor
relevelas in the answer by @StephanLaurent or depending on the desired order one of the several convenience functions in theforcatspackage.However, when doing so you have to use
interactionto get the desired nested axis (as in all examples in the docs, see?guide_axis_nested).However, for your (example) data and accounting for how
weave_factorsworks and differs frominteraction(see below) you could actually achieve your desired result without converting to a factor by switching the order in which you passSpeciesandTxtoweave_factorsand by using the more verboseguide_axis_nested()withinv=TRUE:weave_factorsvs.interaction:weave_factorsdiffers frominteractionin two respects (see? weave_factors:it orders the new levels such that the levels of the first input variable is given priority over the second input.
it treats non-factor inputs as if their levels were
unique(as.character(x)), i.e. the levels are set in the order as in the data (similar to whatforcats::fct_inorderdoes)For that reason
weave_factorsgives IMHO a more natural ordering of the levels of the combined factorsi.e. the levels of combined factor are "ordered" first by the first input, then the second whereas with
interactionit's the other way around: