matrix ordering of heatmap with geom_tile

3,734 views
Skip to first unread message

Gad Abraham

unread,
Nov 3, 2010, 2:48:41 AM11/3/10
to ggplot2
Hi,

I'm trying to make two different heatmaps with geom_tile, which are
column permutations of the same matrix.

I thought that by changing the factor levels the ordering of the tiles
would change along with their labels, but it only affects the labels.
How can I change the tile ordering, without having the labels sorted
alphanumerically by default?

Thanks,
Gad

library(ggplot2)

x1 <- matrix(rnorm(100 * 10), 100, 10)
nm <- paste("X", 1:10, sep="")

cr1 <- cor(x1)
colnames(cr1) <- rownames(cr1) <- nm
crm1 <- melt(cr1)
levels(crm1$X1) <- levels(crm1$X2) <- nm

g1 <- ggplot(crm1, aes(X1, X2)) + geom_tile(aes(fill=value))


s <- sample(10)
x2 <- x1[, s]

cr2 <- cor(x2)
colnames(cr2) <- rownames(cr2) <- nm[s]
crm2 <- melt(cr2)
levels(crm2$X1) <- levels(crm2$X2) <- nm[s]

g2 <- ggplot(crm2, aes(X1, X2)) + geom_tile(aes(fill=value))

pdf("foo.pdf")
print(g1)
print(g2)
dev.off()

Brandon Hurr

unread,
Nov 3, 2010, 6:29:04 AM11/3/10
to Gad Abraham, ggplot2
Gad, 

I could mistaken, but I think you have to use a factor call. 

Something like...

crm2$X1<-factor(crm2$X1, levels= nm[s])

crm2$X2<-factor(crm2$X2, levels= nm[s])


Seems to break the graph though. 


Brandon



--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Brandon Hurr

unread,
Nov 3, 2010, 6:48:02 AM11/3/10
to Gad Abraham, ggplot2
Maybe it didn't break the graph... does the output look like you want it to? Hard to tell with all the randomness. I threw in a reverse on the levels and it seems to work. 

x1 <- matrix(rnorm(100 * 10), 100, 10)
nm <- paste("X", 1:10, sep="")

cr1 <- cor(x1)
colnames(cr1) <- rownames(cr1) <- nm
crm1 <- melt(cr1)
levels(crm1$X1) <- levels(crm1$X2) <- nm

g1 <- ggplot(crm1, aes(X1, X2)) + geom_tile(aes(fill=value))


s <- sample(10)
x2 <- x1[, s]

cr2 <- cor(x2)
colnames(cr2) <- rownames(cr2) <- nm[s]
crm2 <- melt(cr2)
crm2$X1<-factor(crm2$X1, levels= nm[s])
crm2$X2<-factor(crm2$X2, levels= rev(nm[s]))

g2 <- ggplot(crm2, aes(X1, X2)) + geom_tile(aes(fill=value))

pdf("foo.pdf")
print(g1)
print(g2)
dev.off()

HTH,

Brandon
foo.png

Gad Abraham

unread,
Nov 3, 2010, 9:35:09 PM11/3/10
to Brandon Hurr, ggplot2
On Wed, Nov 3, 2010 at 9:48 PM, Brandon Hurr <bhi...@gmail.com> wrote:
> Maybe it didn't break the graph... does the output look like you want it to?
> Hard to tell with all the randomness. I threw in a reverse on the levels and
> it seems to work.

Thanks Brandon, works great.

Cheers,
Gad

--
Gad Abraham
PhD Student, Dept. CSSE and NICTA
The University of Melbourne
Parkville 3010, Victoria, Australia
email: gabr...@csse.unimelb.edu.au
web: http://www.csse.unimelb.edu.au/~gabraham

Reply all
Reply to author
Forward
0 new messages