5

I would like to use my own specific color in my image plot. I am very new in ggplot2 so had a look at its manual from here - the old link does not exist and now is here - and tried to repeat some of the stuff; but I couldn't figure out how to supply my colorbar as I did for the graphics plot.

library(reshape2)
library(ggplot2)

#my specific color list
mycol <- vector(length=512, mode = "numeric")
for (k in 1:256) mycol[k] <- rgb(255, k - 1, k - 1, maxColorValue=255)
for (k in 257:512) mycol[k] <- rgb(511 - (k - 1), 511 - (k - 1), 255, maxColorValue=255)
mycol <- rev(mycol)
ncolors <- length(mycol)

# graphics plot
par(mar = c(5, 13, 1, 6))
image(1:ncol(volcano), 1:nrow(volcano), t(volcano), zlim = c(0, ncolors), col=mycol, axes=FALSE, main="W Matrix", sub = "", xlab= "Components", ylab="Genes")
axis(2, at=1:nrow(volcano), labels=row.names(volcano), adj= 0.5, tick=FALSE, las = 1, cex.axis=0.25, font.axis=1, line=-1)
axis(1, at=1:ncol(volcano), labels=colnames(volcano), adj= 0.5, tick=FALSE,las = 3, cex=1, cex.axis=0.5, font.axis=1, line=-1)


# ggplot2
library(reshape2)
library(ggplot2)
library(ez)
ggplot(melt(volcano), aes(x=Var1, y=Var2, fill=value)) + geom_tile() + scale_color_gradient2(low = muted("red"), mid = "white", high = muted("blue"), midpoint = 0, space = "rgb", guide = "colourbar") # the code does not really use my color bar

Error in unit(tic_pos.c, "mm") : 'x' and 'units' must have length > 0

Areza
  • 5,623
  • 7
  • 48
  • 79
  • 2
    Just replace scale_color_gradient2() with scale_fill_gradient2() as you are using fill= inside aes() – Didzis Elferts Jul 25 '13 at 17:01
  • thanks, how can I use my own colorbar as I did in the lattice plot. – Areza Jul 25 '13 at 17:04
  • I can't reproduce your lattice colorbar because you didn't provide variable myorig – Didzis Elferts Jul 25 '13 at 17:06
  • ...and that isn't a lattice plot, anyhow, its base graphics. Nor do I see how that code could produce a colorbar at all. – joran Jul 25 '13 at 17:07
  • @DidzisElferts I have updated the code. feel free to change rownames/colnames to anything else ! – Areza Jul 25 '13 at 17:20
  • @joran sorry if I called it wrong. I will update the post. from line 5 I am making my colorbar and it is called "mycol". I am using this variable as a parameter in line 12, "col=mycol". – Areza Jul 25 '13 at 17:21
  • I am sorry, might be I should have call it color bar ! should I call it my color list ?! or ? – Areza Jul 25 '13 at 17:22
  • Ok, so you want to know how to use those _specific_ colors in your fill scale in ggplot. – joran Jul 25 '13 at 17:23
  • @joran yes. similar to my graphics plot – Areza Jul 25 '13 at 17:24

2 Answers2

5

Just to clarify @Didzis' answer, which works but may not produce the gradient you're looking for...

'midpoint' refers to the numerical value at which you want the color specified by 'mid' to appear. So, instead of setting the 'midpoint' argument to 256 (which falls outside the range of value, which is the vector you're coloring by), it's wise to set it to a value somewhere in the middle of the range of values you are coloring by, otherwise you aren't using the entire gradient you specified with 'low' and 'high', which defeats the purpose of scale_color_gradient2. The exact value depends on what you are trying to communicate visually, but usually the mean or median is used. Here, I edited @Didzis' code with 'midpoint' set to the median of value

v <- melt(volcano)
ggplot(v, aes(x=Var1, y=Var2, fill=value)) + 
  geom_tile() + 
  scale_fill_gradient2(low = "#0000FF", mid = "#FFFFFF", high ="#FF0000", 
                       midpoint = median(v$value), space = "rgb", guide = "colourbar")

This gives a plot with a much wider gradient:

enter image description here

sc_evans
  • 2,742
  • 1
  • 16
  • 15
0

I think that you should change values for low=, mid= and high= values in scale_fill_gradient2(). For low= I used first value of mycol, for high= last value of mycol and for mid= used 256. value (middle). Also changed midpoint= to 256 as this is midpoint of your number of colors.

ggplot(melt(volcano), aes(x=Var1, y=Var2, fill=value)) + 
  geom_tile() + 
  scale_fill_gradient2(low = "#0000FF", mid = "#FFFFFF", high ="#FF0000", 
                       midpoint = 256.5, space = "rgb", guide = "colourbar")

enter image description here

Didzis Elferts
  • 95,661
  • 14
  • 264
  • 201