If you would like to do this in R, you might want to have a look at the blog post by Oscar Perpiñán Lamigueiro. Its not exactly what you are looking for, but there might be a way to modify it to work for your purposes.
In my last post I described how to produce a multivariate
choropleth map with R. Now I will show how to create a map from raster
files. One of them is a factor which will group the values of the
other one. Thus, once again, I will superpose several groups in the
same map.
Addition:
It may be a bit late, but I thought I would add this as it seems like a relatively simple solution to your problem.
In R, when you use plot()
to display rasters, there is an alpha
argument that sets the transparency. If you plotted your two variables as different color rasters with color scales ranging from light to dark and each alpha
argument set to something like 0.5, the colors would combine where they overlap.
I have done this with spatialPolygons
using spplot
as well. I imagine it would work the same for rasters. Here is an example with spatialPolygons
:
library(sp)
library(rgeos)
library(rworldmap)
box <- readWKT("POLYGON((-180 90, 180 90, 180 -90, -180 -90, -180 90))")
proj4string(box) <- CRS("+proj=cea +datum=WGS84")
set.seed(1)
pts <- spsample(box, n=2000, type="random")
pols <- gBuffer(pts, byid=TRUE, width=50) # create circle polys around each point
merge = sample(1:40, 100, replace = T) # create vector of rand #s between 0-100 to merge pols on
Sp.df <- gUnionCascaded(pols, id = merge) # combine polygons with the same 'merge' value
# create SPDF using polygons and randomly assigning 1 or 2 to each in the @data df
Sp.df <- SpatialPolygonsDataFrame(Sp.df, data.frame(z = factor(sample(1:2, length(Sp.df), replace = TRUE)),
row.names= unique(merge)))
Sp.df <- crop(Sp.df, box)
colors <- c(rgb(r=0, g=0, blue=220, alpha=50, max=255), rgb(r=220, g=0, b=0, alpha=50, max=255))
land <- getMap()
overlay.map <- spplot(Sp.df, zcol = "z", col.regions = colors, col = NA, alpha = 0.5, breaks=c(0,1)) +
layer(sp.polygons(land, fill = "transparent", col = "grey50"))
Obviously, the legend is not very helpful for this map. To create a legend, you then need to plot the gradients in a square with one increasing left to right and the other increasing bottom to top.
Some code to do this in base graphics is here: https://stackoverflow.com/a/11103414/3897439.
I also did it for two colors using ggplot
as I needed grid graphics. Here is a simple example:
Variable_A <- 100 # max of variable
Variable_B <- 100
x <- melt(outer(1:Variable_A, 1:Variable_B)) # set up the data frame to plot from
p <- ggplot(x) + theme_classic() + scale_alpha(range=c(0,0.5), guide="none") +
geom_tile(aes(x=Var1, y=Var2, fill="Variable_A", col.regions="red", alpha=Var1)) +
geom_tile(aes(x=Var1, y=Var2, fill="Variable_B", col.regions="blue", alpha=Var2)) +
scale_x_continuous(limits = c(0, Variable_A), expand = c(0, 0)) +
scale_y_continuous(limits = c(0, Variable_B), expand = c(0, 0)) +
xlab("Variable_A") + ylab("Variable_B") +
guides(fill=FALSE)
p
It's not perfect, but may work for your purposes.
Another source that might have some interesting information relevant to your task is: http://andrewpwheeler.wordpress.com/2012/08/24/making-value-by-alpha-maps-with-arcmap/.