El Dude
El Dude

Reputation: 5618

R Heatmap color x-axis labels by group

I am using the heatmap function to plot a matrix. Each column in the matrix represents one sample, and I have 4 sample types, each with a different quantity. Can I color the labels along the x-axis by sample type?

scaleyellowred <- colorRampPalette(c("lightyellow", "red"), space = "rgb")(10)
heatmap(fitted@H, Rowv = NA, Colv = NA, col = scaleyellowred)

Upvotes: 1

Views: 3949

Answers (2)

Joe
Joe

Reputation: 8611

heatmap.2() from gplots has the colCol argument for this. Set up a vector of colour names corresponding to your sample types. For simplicity, let's say they're grouped together in the original matrix.

m <- matrix(rnorm(400), ncol=40)
sample.types <- c(rep("Blue", 10), rep("Green", 10), rep("Red", 10), rep("Purple", 10))

library(gplots)
heatmap.2(m, trace="none", colCol = sample.types)

enter image description here

Note that the sample types have retained the correct colour when clustered.

Upvotes: 0

AlexT
AlexT

Reputation: 144

Do you mind using ggplot2?..

library(ggplot2)
data("diamonds")
library(dplyr)

diamonds %>% select(cut, color, price) %>% 
        group_by(cut, color) %>% summarize(mean.price=mean(price)) -> data.set
data.set %>% ggplot(data=., mapping=aes(x=cut, y=color, fill=mean.price)) + geom_tile() +
        theme(axis.text.x=element_text(color=rainbow(ncol(data.set))))

Upvotes: 1

Related Questions