This is related to this question about ggplot changing the order of variable names. The answer given there, which manually enforces a bijective reordering of factor levels, does not solve the problem in general. An example is the output of melt(matcor(data.A,data.B)$XYcor), which repeats the rows twice:
data.A <- as.data.frame(matrix(runif(c(25)),nrow=5,ncol=5))
data.B <- as.data.frame(matrix(runif(c(25)),nrow=5,ncol=5))
library(ggplot)
library(CCA)
qplot(x=Var1, y=Var2, data=melt(matcor(data.A,data.B)$XYcor), fill=value, geom="tile")
The idea behind this plot is to show the cross-correlation between two multivariate sets, as img.matcor does:
img.matcor(matcor(data.A,data.B))

In this picture, the upper-left and lower-right quadrants are the autocorrelation matrices of data.A and data.B, while the other quadrants are flipped versions of the cross-correlation matrix. Reordering the data as ggplot2 does destroys this relationship. (On the other hand, ggplot makes this look way better.)
Calling melt with factor.levels=FALSE does not fix this, unfortunately, and in the first place, melt orders the columns properly. Is there a workaround?
