Skip to main content

3D Bar Charts using RGL

I recently wanted to draw some 3D Bar Charts or 3D histograms in R - Cuboids layed out on a rectangular grid. This is very easy in most spreadsheet packages, but doesn't seem to be built into R.

Not to fear, if you install the RGL package (an interface from R to the OpenGL graphics libraries) we can do this and much more. Plus, we can move the figures in real-time by dragging them with the mouse!

library(rgl)
demo(hist3d)

This will produce three interactive plots in their own windows, and show you the R code to do this. In each case I have shown two views - before and after I changed the viewpoint with the mouse. First of all, there is a simple cuboid with a red top plane:

[3D outut from R and RGL] [3D outut from R and RGL]

Then they show you an "exploded cuboid" to show you that its simply made up of six rectangles. The demo code also changes the background colour too:

[3D outut from R and RGL] [3D outut from R and RGL]

Finally, the demo uses a set of cuboids to display a 3D-histogram:

[3D outut from R and RGL] [3D outut from R and RGL]

Very nice... especially when you spin it round and round, but I think it needs a bit of work to use for real data - adding labelled axes for a start.