Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

heatmap-like plot, but for categorical variables

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.

like image 547
seandavi Avatar asked Oct 21 '12 13:10

seandavi


People also ask

Can heatmap be used for categorical variables?

If we want to see how categorical variables interact with each other, heatmaps are a very useful way to do so. While you can use a heatmap to visualize the relationship between any two categorical variables, it's quite common to use heatmaps across dimensions of time.

Which plot is best for categorical variables?

Mosaic plots are good for comaparing two categorical variables, particularly if you have a natural sorting or want to sort by size.

What type of visualization should you use for categorical variables?

Bar Charts and Pie Charts are used to visualize categorical data.

What graphical method is best for categorical data?

Frequency tables, pie charts, and bar charts are the most appropriate graphical displays for categorical variables.


2 Answers

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

like image 74
Tyler Rinker Avatar answered Sep 25 '22 03:09

Tyler Rinker


A similar plot can also be made with base graphics. Here is one method using the base image function. This sample has a categorical response rather than a numeric one.

dx <- data.frame( Tasks = c('1','2','3','4'),
                  Phase1 = c('Done','Done','Done','WIP'),
                  Phase2 = c('WIP','Done','Done',''),
                  Phase3 = c('','WIP','Done',''))

ff<-factor(as.matrix(dx[,2:4]), 
    levels=c("Done","WIP",""), 
    labels=c("done","wip","-empty-")
)
fx<-matrix(as.numeric(ff), ncol=ncol(dx)-1)

#use labels to assign colors
col<-c(done="darkgreen",wip="orange","-empty-"="black")

imgflip<-function(x) {t(x[nrow(x):1,])}

image(imgflip(fx),
    breaks=(1:(nlevels(ff)+1))-.5,
    col=col[levels(ff)],
    xaxt="n", yaxt="n"
)
axis(2, at=seq(0,1,length.out=nrow(dx)), labels=rev(paste("Task",dx$Tasks)), las=2)
axis(3, at=seq(0,1,length.out=length(names(dx))-1), labels=names(dx)[-1])

which will produce this picture.

sample image

like image 38
MrFlick Avatar answered Sep 21 '22 03:09

MrFlick