I have the following content in mydata
:
Class Category
"One" "A"
"One" "A"
"Two" "A"
"Two" "A"
"Three" "B"
"Three" "B"
"One" "C"
"Two" "C"
I use ggplot2
:
ggplot(mydata) +
aes(x = Category, fill = Class) +
geom_bar()
I get this result:
I notice that the "Class" items appear alphabetically. But I want the option to order them as follows:
- ad hoc, so choose exact order
- In order of appearance in data, so in this case,
One, Two, Three
- In reverse order of appearance in data:
Three, Two, One
Answers gratefully appreciated.
Clarification
In case of doubt, here is the full working example of the above data:
Class <- c("One", "One", "Two", "Two", "Three", "Three", "One", "Two", "Four")
Category <- c("A", "A", "A", "A", "B", "B", "C", "C", "C")
mydata <- data.frame(Class, Category)
ggplot(mydata) +
aes(x = Category, fill = Class) +
geom_bar()
The Class key that is generated to the right is in the order:
Four, One, Three, Two
I want to have control over the order of the items in the key produced. (The colors are less important.)