If you have a pair-wise distance matrix which was gained outside R, how could you generate heatmap based on this matrix. Here is an example
1. The pair-wise distance matrix outside R
2. This is the heatmap generated by R ggplot2 package.
3. R code
# load packages needed
library(ggplot2)
# read in the data frame including pair-wise distances
dist.m <- read.csv("dist_matrix.csv", header=T)
head(dist.m) # in fact, dist.m is a data.frame of R
# convert the data.frame of distance to a real matrix of distance
# be care of setting the right row/column names
dist.m2 <- as.matrix(dist.m[,2:13], dimnames=lsit(as.vector(dist.m[,1]), as.vector(dist.m[,1])))
row.names(dist.m2) <- as.vector(dist.m[,1])
head(dist.m2)
dist.m2[1,1]
# convert the distance matrix to data.frame
dist.m3 <- melt(dist.m2)
dist.m3 <- dist.m3
head(dist.m3)
# set the order of rows/columns
dist.m3$X1 <- factor(dist.m3$X1, levels = c("Ath", "AL3b", "AL13a", "AL34a", "AL25b", "AL8b", "AL10b",
"AL38a", "AL35a", "AL4a", "AL17a", "AL16a"))
dist.m3$X2 <- factor(dist.m3$X2, levels = c("Ath", "AL3b", "AL13a", "AL34a", "AL25b", "AL8b", "AL10b",
"AL38a", "AL35a", "AL4a", "AL17a", "AL16a"))
# set up heatmap plot
pdf("heatmap.pdf")
g.m <- ggplot(dist.m3, aes(X1, X2)) + geom_tile(aes(fill=value))
g.m
dev.off()
没有评论:
发表评论