0

Here is the challenge that I am currently facing.

I have a data.frame named "dx"

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

I would want to use the data in the dataframe dx and then color the data frame based on the value that a given cell contains. E.g: "Done" to be colored in Red, Blank Cells to be colored in Black etc.)

Note I don't want to see text in the cells instead the colors that user defines. The final output would resemble like a conditional function applied table in Excel file.

enter image description here

A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
  • 1
    So the output of this will be an image right? – Paulo E. Cardoso May 08 '14 at 17:35
  • What you are looking for is a heatmap. Please have a look at http://stackoverflow.com/questions/12998372/heatmap-like-plot-but-for-categorical-variables – Beasterfield May 08 '14 at 17:41
  • The example that you shared uses numerical values and plots a heatmap, but in my case the values are not numerical its a string and I would want to associate a string value to a color. Hope I am able to explain my request clearly. – user2028303 May 08 '14 at 17:47
  • yes the output can be an image or an output which can be easily rendered using R shiny package – user2028303 May 08 '14 at 17:51

1 Answers1

3

Here is one method using the base image function.

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

MrFlick
  • 195,160
  • 17
  • 277
  • 295