Best way to order boxes in a lattice bwplot by their median

643 Views Asked by At

My goal is to order the boxes in a lattice bwplot by their median. I know that aggregate, reorder, etc., as well as the lattice index.cond or perm.cond parameters can be used for this this purpose, but I struggle putting the pieces correctly together with bwplot. I would appreciate an answer that gives a clean and reusable solution, and states some details about how the parameters and functions work together in that solution - because this is what is don't fully understand right now.

This is a minimal working example of the plot without ordering:

bwplot(data = iris, x = Sepal.Width ~ Species)

To my understanding, ordering the boxes by the median could be enforced by something similar to this:

# notrun
bwplot(data = iris, x = Sepal.Width ~ Species | XXX, index.cond = function(x,y) reorder(x,y,median))
bwplot(data = iris, x = Sepal.Width ~ Species, perm.cond = XXX)

PS: the bunch of existing questions on SO related to ordering a bwplot (at least those I found) either investigate other details, or did not lead to a generic solution for me.

1

There are 1 best solutions below

3
Adam Quek On BEST ANSWER
bymedian <- with(iris, reorder(Species, Sepal.Width, median))
lattice::bwplot(Sepal.Width ~ bymedian, data=iris)

enter image description here