Arbitrary reordering of histogram columns in R

I would like to know how I can reorder the histogram columns in a way that makes sense for my data. This example illustrates what I'm trying to do.

I have this data in a file:

blue    low
blue    medium
blue    high
blue    high
blue    high
blue    medium
green   low
green   low
green   low
green   high
pink    low
pink    high
pink    medium
pink    low
pink    high
red     high
red     low
red     low
red     low
red     medium
red     medium
red     medium

If I run the following commands:

colours <- read.table("colours.txt", sep="\t")
library(lattice)
histogram(~ V2 | V1, data=colours,  type="count")

I get pretty much what I want, except that the columns in the histograms are sorted alphabetically, high, low, medium, and I would like them to be sorted in a more natural way, low, medium, high.

Many thanks in advance for any pointers on how to do this.

+3
source share
1 answer

You just need to indicate your factors:

colours$V2 = factor(colours$V2, levels=c("low", "medium", "high"))
histogram(~ V2 | V1, data=colours,  type="count")
+2
source

All Articles