I’ve run two experiments, some replicating the conditions of a previous set. I have a column for the unique lot ID as well as another column containing the ID from experiment 1 which the lots from experiment 2 replicates. Here’s some example data stored like this:
test <- data.frame(var1=c(rep("A",4), rep("B",4), rep("C",4), rep("D",4)),
var2=rep(c(rep("A",4), rep("B",4)),2),
value=runif(16,1,5))
Here’s my ggplot code:
ggplot(test, aes(x=var1, y=value, fill=var2)) + geom_boxplot()
This gives me the lot IDs arranged according to var1‘s factor order.

I’d like the A’s from var2 side by side and the B’s from var2 side by side. Is the only way to do this by using facet_grid or facet_wrap?
ggplot(test, aes(x=var1, y=value, fill=var2)) + geom_boxplot() +
facet_grid(. ~ var2, scales="free_x")

I tried adding group=var2 but that gives me overlapping and very wide boxplots, which I also don’t understand:
ggplot(test, aes(x=var1, y=value, group=var2, fill=var2)) + geom_boxplot()
Warning message:
position_dodge requires non-overlapping x intervals

I can use facetting; I’m mostly asking the question as I was surprised when I couldn’t group how I expected. I also looked in the examples for geom_bar() for more grouping approaches but it seems most people aren’t grouping things this way.
Feel free to provide other input on how one might approach this. I’m simply looking to compare pairs of test results to make it easy to see whether the replicate matches the original trial.
A cheats method using
interactionand adjusting thescale_x_discretelabelsEDIT Thanks to @Andrie’s comments
Or you can create an appropriately ordered factor, or (unordered) factor with levels specified in the correct order.
orderedfactor or not)..
Will both give the same result
EDIT — Another approach
You could have your x axis as
var2and fill byvar1, which will order byvar2then dodge and fill byvar1