0

UPDATE: Deleted now irrelevant preamble and updated progress.

Hi I'm trying to plow a horizontal bar a bit like a heat-map, which will change color based on values below. The bar should be red by default, but changes color depending on the values of two parameters.

I've found some pages that try for a similar effect on SO: R matrix plot with colour threshold and grid

How can one mix 2 or more color palettes to show a combined color value

So I figure the best way to go about this would be to plot a series of tiles horizontally, and each one has a calculated color based on two values.

What I have managed to do, is generate the colors I want. I did this by getting a matrix[2xn], where n is the number of tiles. row1 contains value1 for each tile, and row 2 contains value 2 for each tile:

 colourmatrix[,c(1:5)]
     [,1]      [,2]      [,3]      [,4]      [,5]     
     "81"      "82"      "82"      "82"      "82"     
     "58"      "58"      "58"      "58"      "58"

I then generated the colors for each tile by applying the following to all columns:

col.deter <- function(invalues) {
  cols <- rgb(green=invalues[1], red=100, blue=invalues[2], maxColorValue=100)
  return(cols)
}

cols <- unlist(lapply(1:ncol(colourmatrix), function(i) col.deter(colourmatrix[,i])))

Then I do an rbind() my results is:

 > colourmatrix[,c(1:5)]
     [,1]      [,2]      [,3]      [,4]      [,5]     
     "81"      "82"      "82"      "82"      "82"     
     "58"      "58"      "58"      "58"      "58"     
cols "#FFCF94" "#FFD194" "#FFD194" "#FFD194" "#FFD194"

So for each tile, the higher value 1, the more green is added to the mix, the higher value 2, the more blue is added to the mix. Thus the color for every tile is determined and added as a third row.

What I want to do now, is something like the grid generated in this page with ggplot2 in the answers with this code:

dat <- expand.grid(blue=seq(0, 100, by=10), red=seq(0, 100, by=10))
dat <- within(dat, mix <- rgb(green=0, red=red, blue=blue, maxColorValue=100))

library(ggplot2)
ggplot(dat, aes(x=red, y=blue)) + 
  geom_tile(aes(fill=mix), color="white") + 
  scale_fill_identity()

but imagine only one line. I'm trying to achieve one line of tiles going left to right, their color determined by the values generated.

I have attempted to do this with ggplot2 and image() but my attempts fall flat, and I don't normally use ggplot 2 I don't understand aes and all the errors I get, but I tried it since that's what was used to generate the red and blue tile plot produced by the code chunk above.

My failed attempts:

ggplot(c(1,ncol(colourmatrix)), 1, aes(x=x, y=y)) + 
  geom_tile(aes(fill=colourmatrix[3,]), color="white") + 
  scale_fill_identity()

Error in ggplot.data.frame(fortify(data, ...), mapping) : 
  Mapping should be created with aes or aes_string


  qplot(c(1,ncol(colourmatrix)), c(1,ncol(colourmatrix)), fill=colourmatrix[3,], geom='tile')

Error: Aesthetics must either be length one, or the same length as the dataProblems:c(1, ncol(colourmatrix)), c(1, ncol(colourmatrix))

I've also got some failed attempts trying image() pages I've read to try and achieve this are:

How can one mix 2 or more color palettes to show a combined color value

R matrix plot with colour threshold and grid

Thanks,

Ben W.

Community
  • 1
  • 1
SJWard
  • 3,629
  • 5
  • 39
  • 54
  • Do you plan on doing much custom visualization in the future? If so, you may want to knuckle down and learn the base 'grid' package. With it, you can create **any** visual, but it takes a little bit longer to set up at first. For highly custom visualizations, though, it's WAY faster than fighting with wrappers like the 'ggplot' functions. – Dinre Mar 05 '13 at 19:28

1 Answers1

0

What about something like this (or a version where your total similarity at a given window location is normalized)? R - ggplot2, several questions, multiple correlated plots

EDIT: This would be for just one string (that is, you'd make one for each of A, B, C).

Community
  • 1
  • 1
Paul J Hurtado
  • 572
  • 1
  • 4
  • 12