21

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?

Paul Hiemstra
  • 58,502
  • 12
  • 138
  • 147
Seb
  • 5,265
  • 7
  • 29
  • 49
  • 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 Jan 04 '12 at 01:47
  • @hadley: great thank you! that probably would have been my second question ;) – Seb Jan 04 '12 at 06: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 – mindlessgreen Apr 03 '12 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! – Seb Apr 03 '12 at 16:50

2 Answers2

34

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.

csgillespie
  • 57,032
  • 13
  • 142
  • 178
Paul Hiemstra
  • 58,502
  • 12
  • 138
  • 147
  • 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? – Seb Jan 03 '12 at 14:29
  • You can change the order of the levels for each variable in the data.frame separately. Just reverse the ones you need. – Paul Hiemstra Jan 03 '12 at 14:31
  • sure! thanks - the product of the `melt()` command was the point to change! excellent and fast solution - as allways. the clarification helps in addition! – Seb Jan 03 '12 at 14:37
6

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

Andy
  • 4,299
  • 30
  • 26