Ggplot2 heatmap: how to save tag order?

I am trying to build a heatmap in ggplot2 using csv data after casbon's solution

http://biostar.stackexchange.com/questions/921/how-to-draw-a-csv-data-file-as-a-heatmap-using-numpy-and-matplotlib

The x-label problem is trying to re-sort itself. For example, if I replace the label COG0002 and COG0001 in this example data, the x-label still comes out in sort order (cog0001, cog0002, cog0003 .... cog0008).

Is there anyway to prevent this? I want to be ordered, as in the csv file

thanks

R

+6
r ggplot2 heatmap
source share
3 answers

If you want to save the order directly from the csv file:

foomelt$COG <- factor(foomelt$COG, levels = as.character(foo[[1]])) 
+4
source share

If I recall, when calling factor (x) with the level argument, the default levels are set as levels = sort (unique (x)).

You can override this action by setting levels = unique (x).

For example:

 set.seed(1) x = sample(letters, 100, replace = TRUE) head(x, 5) 

[1] "g" "j" "o" "x" "f"

 levels(factor(x)) 

[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p "" q "" r "" s "

[20] "t" "u" "v" "w" "x" "y" "z"

 levels(factor(x, levels = unique(x))) 

[1] "g" "j" "o" "x" "f" "y" "r" "q" "b" "e" "u" "m" "s" "z" "d" "k "" a "" w "" i "

[20] "p" "v" "c" "n" "t" "l" "h"

You can see that the setting level = unique (x) preserves the order of entry into the data.

+5
source share

Have you tried reordering factor levels before plotting? eg.

 foomelt$COG = factor(foomelt$COG,levels(foomelt$COG)[c(2,1,3:8)]) 

(I can’t try right now, so I can’t be sure that it works)

+2
source share

All Articles