Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2: change order of display of a factor variable on an axis

Tags:

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?

like image 433
Seb Avatar asked Jan 03 '12 14:01

Seb


2 Answers

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.

like image 140
Paul Hiemstra Avatar answered Oct 07 '22 20:10

Paul Hiemstra


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

like image 44
Andy Avatar answered Oct 07 '22 21:10

Andy