# How can I create a 3-dimensional cube legend (R)?

Has anyone ever plotted a 3-D (cube) legend as shown below? Essentially trivariate mapping.

I would like to recreate the below using the same variables of population, built (% area that is built up) and light (a variable already derived from satellite imagery). I'm interested in recreating the cube itself and the labels. The explanation boxes would be a bonus but not critical!  (The reference for the above image is EU Commission as part of the Global Human Settlements Layer: https://twitter.com/vladimirsucha/status/1140292484695764995)

This function generates an approximation to that legend using the 3d capabilities of the `rgl` package:

``````library(rgl)
key3d <- function(){
s = seq(0, 1, len=5)
xyz = expand.grid(x=s, y=s, z=s)
## this order replicates the example
col = rgb(xyz[,2], xyz[,1], xyz[,3])
bg3d(col="black")
## m can be got from the plot with par3d()\$userMatrix
m = structure(c(-0.692, -0.426, 0.582, 0,
0.722, -0.395, 0.569, 0,
0, 0.814, 0.581, 0,
0, 0, 0, 1), .Dim = c(4L, 4L))
view3d(userMatrix=m)
}
`````` Note that it uses spheres instead of cubes (cubes could be done but are a bit more work because each cube can have its own rotation). The matrix `m` constructed in the code sets the view point and rotation, and is near enough to looking straight down that axis - the exact values probably need some trigonometry to work out but this is close enough.

Adding text to a `rgl` plot can be done using `text3d`. Its a bit fiddly to get right so here's one corner for starters. You might need to play with alignments and position to get it perfect:

``````key_labels <- function(){ • If you want to plot the map with this colour scheme you write a function that turns your three variables to a a colour - in this case one variable is the amount of red, the second green, the third blue (or some permutation). If you rescale your variables to (0,1) you can use the `rgb` function like I did to create the legend. Or for rasters you layer 3 rasters to create an appropriately scaled RGB raster and the colours popout automatically. Maybe ask a new question on colouring a raster map with a trivariate scheme if you can't find an answer online? Jan 18 at 12:04