heatmap-like plot, but for categorical variables

seandavi picture seandavi · Oct 21, 2012 · Viewed 16.5k times · Source

I have three factors (set1, set2, and set3) for each of about 50 individuals. The values for set1, set2, and set3 are "A","B","C". I'd like to make a heatmap-like plot of these data but have the legend show the color associated with the values (eg., A='red', B='blue', C='black'). Any suggestions?

Thanks.

Answer

Tyler Rinker picture Tyler Rinker · Oct 21, 2012

I decided it would be easist to approach this with ggplot2 (for me anyway):

#recreate a data set
dat <- data.frame(person=factor(paste0("id#", 1:50), 
    levels =rev(paste0("id#", 1:50))), matrix(sample(LETTERS[1:3], 150, T), ncol = 3))

library(ggplot2); library(reshape2)
dat3 <- melt(dat, id.var = 'person')
ggplot(dat3, aes(variable, person)) + geom_tile(aes(fill = value),
   colour = "white") + scale_fill_manual(values=c("red", "blue", "black"))

enter image description here