I want to overlay two ggplot2
plots with alpha channels in a way that the resulting image shows both datasets. This is my test data:
data = read
Here is exactly the same solution than @Roland, excepting that I suggest controur line. This allow you to appreciate the overlapping. I can't see how geom_tile and your idea of "multiplication" could enable you to appreciate that. Maybe if you use blue and red for none-overlapping area, and a "weighted" violet color for overlapping area. But I guess you would have to compute it in a previous step before ploting I guess.
ggplot(rbind(data.frame(data, group="a"), data.frame(data2, group="b")),
aes(x=x,y=y)) +
stat_density2d(geom="density2d", aes(color = group,alpha=..level..),
size=2,
contour=TRUE) +
#scale_color_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))
You should plot both densities on the same scale:
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))
Otherwise you display a distorted picture of your data.