2

I imported my data into R and melted it to make a geom_tile heat map. My input file looks like this:

sample,variable,value
chickenpox,person1,7
measles,person1,5
mumps,person1,2
diptheria,person1,8
chickenpox,person2,3
measles,person2,4
mumps,person2,0
diptheria,person2,8
chickenpox,person3,1
measles,person3,2
mumps,person3,7
diptheria,person3,2
chickenpox,person4,1
measles,person4,2
mumps,person4,5
diptheria,person4,1

Then I run:

b<-read.csv(file.choose(),header=TRUE)
q <- ggplot(data=b, aes(x=variable, y=sample, fill=value))
q + geom_tile()

And I am given this picture: Imgur

The problem is: The y axis automatically alphabetizes it. My data shouldn't be in alphabetical order, but in a specific order. How can I have the heat map display sample as the y-axis and variable as the x-axis, while maintaining the imported order of sample?

It would be a major hassle for me to retype the sample column for each of the 460 factors that sample has for each person.

There has to be a faster way.

Phil Colson
  • 141
  • 3
  • 9
  • Could you add a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)? – Jaap May 15 '14 at 13:49
  • 4
    If you just want to change the order, use `scale_y_discrete` to specify it. Something like `ggplot(df, aes(x=variable, y=sample, fill=value)) + geom_tile() + scale_y_discrete(limits=unique(df$sample))` should work. – shadow May 15 '14 at 14:06
  • Thanks shadow! That made it work! – Phil Colson May 15 '14 at 14:21

0 Answers0