The following code plots the `price`

of the diamonds as a function of `carat`

and `depth`

using a 2d binning.

```
library(ggplot2)
data(diamonds)
gp <- ggplot(diamonds,aes(x=carat,y=depth,z=price))
gp <- gp +stat_summary_2d()
gp
```

I would now like represent, not just the price, but also another continuous variable, say `x`

, as a different color channel. So the intensity of blue would give me the `price`

and the intensity of red would give the `x`

(and potentially a third variable coded in the green channel).

What is the best way the achieve this? Do I have to manually bin the data, compute the summary and plot the resulting raster, or is there a quicker way?

Or is it possible to do it on three different plots using the `z`

value and then merge them by assigning each of them to a different color channel?

**Update**
For a more explicit example, the following code generates three plots (see below). I would like to merge them into one plot, each one associate with one color channel, so that I would have one red blob, one green blob and one blue blog in a single plot.

```
library(ggplot2)
n <- 10000
cx <- c(-1, 0, 1)
cy <- c(0,1,-1)
x <- rnorm(n,0,1)
y <- rnorm(n,0,1)
v <- list()
v <- lapply(seq(3),function(i)dnorm(x,cx[i],0.5)*dnorm(y,cy[i],0.5))
data <- data.frame(x,y,v1=v[[1]]/max(v[[1]]),v2=v[[2]/max(v[[2]]), v3=v[[3]]/max(v[[3]]))
gp1 <- ggplot(data, aes(x=x,y=y,z=v1)) + stat_summary_2d() + scale_colour_identity()
gp2 <- ggplot(data, aes(x=x,y=y,z=v2)) + stat_summary_2d() + scale_colour_identity()
gp3 <- ggplot(data, aes(x=x,y=y,z=v3)) + stat_summary_2d()+ scale_colour_identity()
```

Making use of the

`layer_data()`

function we can grab whatever value is computed on a layer and use it how we want. Let's assume we already have the three plots in your example;`gp1`

,`gp2`

and`gp3`

.Let's save the colour values before the hexadecimal transformation in a new data.frame:

Since these are computed densities, it may be best to rescale them to be within [0,1] first before we transform them to colour hexademical notation:

Now, since the x and y data between each of the plots are the same, we can just grab the x-y coordinates from one of them and combine it with our new colours:

Because our colours are already in the format that ggplot understands to interpret as colours, we'll use the

`scale_fill_identity()`

for plotting this:Which produced the following for me:

Alternatively, we can also plot each colour channel as a layer and use

`alpha`

to mix these:Which gave me the following:

However, keep in mind that the order in which you add the layers is going to affect the look of the plot. In my case, blue came last, so there is a blue sheen over all the other values.

EDIT: The sheen can be largely removed by adding

`scale_alpha_continuous(range = c(0,1))`

. The resulting plot looks a lot like the next method, but doesn't mix red and green into bright yellow, which I would argue is more realistic. The extent of the data can no longer be estimated though! (end edit)Another approach to the alpha strategy that avoids a dominant colour's sheen is to map the rgb values to hsv space, keeping the 'v' constant and set an alpha to the sum of the rows:

A question that you can ask yourself with that method however, is how accurately you think the yellow bit in between the red and green bit is in representing your data? Also, because we no longer have the background shape, we cannot see anymore what the extent of the data is.

Be aware that not every export method supports having alpha values for colours.

EDIT: I know of no graceful legend solutions unfortunately, if anyone does, please let me know!