21
votes

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:

enter image description here

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?

2
You might also want to add a limits = c(0, 1) to your current scale_colour_gradient command - currently 1 is outside the limits of the scale and isn't coloured correctly in the legend.hadley
@hadley: great thank you! that probably would have been my second question ;)Seb
How to not order the y labels at all? If I have months for example J,F,M,A,M. It gets sorted automatically. Is it possible to override this default behaviour? Thanksrmf
@Roy as far is I can remember it is a matter of factor levels. but this may be worth creating a new question!Seb

2 Answers

34
votes

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.

6
votes

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:

enter image description here