ggplot2: change order of display of a factor variable on an axis
When I use geom_tile()
with ggplot2
and discrete scales the labels are in ascending order on the x-axis and in descending order on the y-axis:
#some sample data
a <- runif(400)
a <- matrix(a, ncol=20)
colnames(a) <- letters[seq( from = 1, to = 20 )]
rownames(a) <- letters[seq( from = 1, to = 20 )]
a <- melt(a)
When I plot the dataframe a
this comes out:
ggplot(a, aes(X1, X2, fill = value)) + geom_tile() +
scale_fill_gradient(low = "white", high = "black", breaks=seq(from=0, to=1, by=.1), name="value") +
opts(axis.text.x=theme_text(angle=-90, hjust=0)) +
scale_x_discrete(name="") + scale_y_discrete(name="")
and the coords are labeled differently for x and y:
I would like to have the labels sorted from a-z from top to bottom and from left to right. is there a quick way to do this?
Solution 1:
The important point here is the order of the factor levels. The order in the levels is also the order in the plot. You can use rev
to reverse the order of the levels like this (note that I just reorder one column in a data.frame):
df$X1 = with(df, factor(X1, levels = rev(levels(X1))))
Use this syntax to reorder your factors as needed.
Solution 2:
For the cases where you would prefer to not modify the order of the factor in underlying data, you can get the same result using the limits
argument to scale_y_discrete
:
ggplot(a, aes(X1, X2, fill = value)) +
geom_tile() +
scale_y_discrete(name="", limits = rev(levels(a$X2)))
Giving this output: