How does the formula in the first parameter of boxplot in the following code make the correct correspondence between b and a after a has been reordered.
a <- as.factor(c("TX", "NY", "WA"))
levels(a)
b <- c(5, 3, 2)
boxplot(b ~ a)
# Order the levels of a according to their value in b
a_reordered <- reorder(a, b)
levels(a_reordered)
boxplot(b ~ a_reordered)
Why doesn’t b need to be reordered as well?
edit: I replaced my example with the concrete example of @Marius
In your
boxplot(quantity ~ bymedian)call, the order of states on the x-axis is determined by the order of levels for thebymedianfactor. Comparelevels(x$State)tolevels(bymedian), and you’ll see why the two variables behave differently when used in a plot. Note that the data itself inbymedianhasn’t changed order.A quick example:
And just to make it clear what it means to say that the actual data hasn’t changed: