title: Overlay two ggplot2 stat_density2d plots with alpha channels
body: <p>I want to overlay two <code>ggplot2</code> plots with alpha channels in a way that the resulting image shows both datasets. This is my test data:</p>
  
  <pre><code>data = read.table(text="P1 -1 0 4\nP2 0 0 2\nP3 2 1 8\nP4 -2 -2 6\nP5 0.5 2 12")
  data2 = read.table(text="Q1 1 1 3\nQ2 1 -1 2\nQ3 -1 1 8")
  colnames(data) = c("name","x","y","score")
  colnames(data2) = c("name","x","y","score")
  </code></pre>
  
  <p>And here is how I plot this data:</p>
  
  <pre><code>ggplot(data, aes(x=x,y=y)) + 
    stat_density2d(data=data,geom="tile", aes(fill = ..density..,alpha=..density..), contour=FALSE) + 
    theme(legend.position="none") + scale_fill_gradient (low = "#FFFFFF", high = "#FF0000") + 
    xlim(-3,3) + ylim(-3,3) + 
    geom_point()
  
  ggplot(data2, aes(x=x,y=y)) + 
    stat_density2d(data=data2,geom="tile", aes(fill = ..density..,alpha=..density..), contour=FALSE) + 
    theme(legend.position="none") + 
    scale_fill_gradient (low = "#FFFFFF", high = "#00FF00") + 
    xlim(-3,3) + ylim(-3,3) + 
    geom_point()
  </code></pre>
  
  <p>The first plot shows <em>data</em>, the second plot <em>data2</em>:</p>
  
  <p><img src="https://i.stack.imgur.com/l59Zb.png" alt="Plot for dataset *data*">  <img src="https://i.stack.imgur.com/Gp6tT.png" alt="Plot for dataset *data2*"></p>
  
  <p>I now want a combination of both plots. The following image is what I want to get. I produced it with my image editing program on the desktop by multiplying both images as layers.</p>
  
  <p><img src="https://i.stack.imgur.com/IkgjV.png" alt="Both datasets in one plot"></p>
  
  <p>I tried to plot one dataset on top of the other, but that doesn't multiply both layers and the second color overwrites the first one.</p>
  
  <pre><code>ggplot(data, aes(x=x,y=y)) + 
    stat_density2d(data=data,geom="tile", aes(fill = ..density..,alpha=..density..), contour=FALSE) + 
    theme(legend.position="none") + scale_fill_gradient (low = "#FFFFFF", high = "#FF0000") + 
    xlim(-3,3) + ylim(-3,3) + 
    stat_density2d(data=data2,geom="tile", aes(fill = ..density..,alpha=..density..), contour=FALSE) + 
    scale_fill_gradient (low = "#FFFFFF", high = "#00FF00")
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/NYZ4I.png" alt="enter image description here"></p>
  
  <p>Additionally I get this warning: <em>Scale for 'fill' is already present. Adding another scale for 'fill', which will replace the existing scale.</em></p>
  
  <p>Is there a way to do this in R? Or is there another way (using other functions like eg. smoothScatter) to get this or a similar result? As a kind of workaround I think I'll get a similar result using ImageMagick on the server, but I'd prefer to do it all in R.</p>
  
  <p><strong>Update 1</strong></p>
  
  <p>The multiplication of two layers is done in ImageMagick this way;</p>
  
  <pre><code>composite -compose multiply data-red.png data-green.png im-multiply.png
  </code></pre>
  
  <p>This gives the same result as shown above.</p>
  
  <p><strong>Update 2</strong></p>
  
  <p>@Roland taught me in his answer how to plot the two datasets within the same plot. While this is great, one problem remains: The image depends on the order you feed the data to the plot.</p>
  
  <pre><code>ggplot(rbind(data.frame(data, group="a"), data.frame(data2, group="b")), aes(x=x,y=y)) + 
    stat_density2d(geom="tile", aes(fill = group, alpha=..density..), contour=FALSE) + 
    scale_fill_manual(values=c("a"="#FF0000", "b"="#00FF00")) + 
    geom_point() + 
    theme_minimal() + 
    xlim(-3.3, 3.3) + ylim(-3.3, 3.3) +
    coord_cartesian(xlim = c(-3.2, 3.2), ylim = c(-3.2, 3.2))
  </code></pre>
  
  <p>gives this result:</p>
  
  <p><img src="https://i.stack.imgur.com/okt4B.png" alt="First plot dataset &quot;a&quot; then dataset &quot;b2&quot;."></p>
  
  <p>When swapping the order of both datasets (now dataset "b" aka <em>data2</em> comes first, then dataset <em>data</em> aka "a"), you get a similar result, but now the red color dominates, because it get's plotted later and thus kind of overwrites the green data.</p>
  
  <pre><code>ggplot(rbind(data.frame(data2, group="a"), data.frame(data, group="b")), aes(x=x,y=y)) + 
    stat_density2d(geom="tile", aes(fill = group, alpha=..density..), contour=FALSE) + 
    scale_fill_manual(values=c("b"="#FF0000", "a"="#00FF00")) +
    geom_point() + theme_minimal() + 
    xlim(-3.3, 3.3) + ylim(-3.3, 3.3) + 
    coord_cartesian(xlim = c(-3.2, 3.2), ylim = c(-3.2, 3.2))
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/kHGGQ.png" alt="enter image description here"></p>
  
  <p>I need a solutions that doesn't depend on the order of the datasets.</p>