Search code examples
r3dhistogramoverlapdensity-plot

How to plot two bivariate histograms in a single frame?


I am trying to plot two bivariate histograms in a single frame to see if they are equal. I have put an MWE below with randomly generated data.

x1 = seq(1, 5, length.out = 20)
x2 = seq(1, 5, length.out = 20)
x3 = matrix(rnorm(400), 20, 20)

y1 = seq(1, 5, length.out = 20)
y2 = seq(1, 5, length.out = 20)
y3 = matrix(rcauchy(400), 20, 20)

persp(x1, x2, x3, theta = 30, phi = 30, expand = 0.5, col = "lightblue")
par(new = TRUE)
persp(y1, y2, y3, theta = 30, phi = 30, expand = 0.5, col = "green")

The plot looks like

enter image description here

So, the plots overlap in such a way that I can not compare them. Can I make any one of the plots somehow transparent to do so? Thanks in advance!


Solution

  • You may use rgb() with option alpha for col.

    persp(x1, x2, x3, theta=30, phi=30, expand=0.5, col=rgb(0,0,1,alpha=0.3) )
    par(new=TRUE)
    persp(y1, y2, y3, theta=30, phi=30, expand=0.5, col=rgb(0,1,0,alpha=0.3) )
    

    Result

    enter image description here

    Note: To avoid labels overlap, add xlab="", ylab="", zlab="" options e.g. to the second plot.