Cluster data in heat map in R ggplot
You can achieve this by defining the order of Timepoints in a dendrogram after you have applied hclust
to your data:
data <- scale(t(data))
ord <- hclust( dist(data, method = "euclidean"), method = "ward.D" )$order
ord
[1] 2 3 1 4 8 5 6 10 7 9
The only thing you have to do then is transforming your Time-column to a factor
where the factor levels are ordered by ord
:
pd <- as.data.frame( data )
pd$Time <- sub("_.*", "", rownames(pd))
pd.m <- melt( pd, id.vars = "Time", variable.name = "Gene" )
pd.m$Gene <- factor( pd.m$Gene, levels = colnames(data), labels = seq_along( colnames(data) ) )
pd.m$Time <- factor( pd.m$Time, levels = rownames(data)[ord], labels = c("0h", "0.25h", "0.5h","1h","2h","3h","6h","12h","24h","48h") )
The rest is done by ggplot
automatically:
ggplot( pd.m, aes(Time, Gene) ) +
geom_tile(aes(fill = value)) +
scale_fill_gradient2(low=muted("blue"), high=muted("red"))
I don't think ggplot
supports this out of the box, but you can use heatmap
:
heatmap(
as.matrix(dat), Rowv=NA,
Colv=as.dendrogram(hclust(dist(t(as.matrix(dat)))))
)
Note this won't look like yours because I'm just using the head
of your data, not the whole thing.
Here we specify the clustering manually with a dendogram derived from your hclust
with the Colv
argument. You can specify the clustering manually too through the Colv
argument if the one used by default doesn't line up with what you want.