22

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?

4
  • 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. Commented Jan 4, 2012 at 1:47
  • @hadley: great thank you! that probably would have been my second question ;) Commented Jan 4, 2012 at 6:58
  • 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? Thanks Commented Apr 3, 2012 at 13:12
  • @Roy as far is I can remember it is a matter of factor levels. but this may be worth creating a new question! Commented Apr 3, 2012 at 16:50

2 Answers 2

36

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.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for your fast help! But doesn't that change the x and the y axis. The x-axis should stay like it is. Or did I get something wrong?
You can change the order of the levels for each variable in the data.frame separately. Just reverse the ones you need.
sure! thanks - the product of the melt() command was the point to change! excellent and fast solution - as allways. the clarification helps in addition!
8

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.