How does one plot a 3D stacked histogram in R?

10,558

Solution 1

You could try using either rgl (see here) or 3dscatterplot (as in this example). Lattice also supports this:

library(lattice) 
library(latticeExtra) 
?panel.3dbars

You can see an example of this on the Learnr blog.

I don't believe that's technically a stacked histogram (a stacked histogram stacks the bars on top of each other). Moreover, a different kind of histogram could be more informative: look at the ggplot2 the documentation here for some examples.

 hist_cut <- ggplot(diamonds, aes(x=price, fill=cut)) 
 hist_cut + geom_bar() # defaults to stacking 

Another option is to use latticing instead, with facet_wrap in ggplot2 (see this post as an example).

Solution 2

One doesn't. This is a terrible display of data because the front histograms obscure the rear histograms and the perspective makes it just about impossible to read the values off the y-axis.

Share:
10,558
Admin
Author by

Admin

Updated on June 14, 2022

Comments

  • Admin
    Admin about 2 years

    I want to plot stacked histograms in R; i.e. stack individual histograms in the third dimension.


    thank you all for your suggestions, especially the one by Shane.

    @hadley, I agree with your points, however, my situation is different: the main point I'm trying to convey by plotting four stacked histograms is that the tails vary significantly....the part that will get obscured is of no consequence in the data I'm presenting....also, being able to read the frequency axis is also not important since I'll be plotting the relative frequencies...

  • Shane
    Shane over 14 years
    I agree 100% with this as well: that said, as Duncan Murdoch has said on several occasions, if one is going to use a 3D plot like this then using the rgl package so that it is interactive can help to address some of these concerns. Rotating the plot can reduce the obscuring effect.
  • Pete
    Pete about 11 years
    Except in cases where the front histograms don't obscure the rear histograms and the perspective makes everything absolutely clear
  • Timothée HENRY
    Timothée HENRY over 10 years
    Would side by side histograms be the right way to compare histograms (refer stackoverflow.com/questions/16273724/…) ?