How to do a 3D Bar Chart (if possible)

Does anyone know how to do a 3D Bar Chart in R?
Tanks :slight_smile:

@HansPeter123

Plotly does not provide a 3d bar chart, but you can define it yourself as follows(I summarize here how I did it in Python):

Define the vertices of the unit cube as an array of elements:

[[0, 0, 0],
[1, 0, 0],
[1, 1, 0],
[0, 1, 0],
[0, 0, 1],
[1, 0, 1],
[1, 1, 1],
[0, 1, 1]]
  • find the xmin, xmax ymin ymax of your 2d data

  • fix a number of bins in the x, respectively y direction (depending on xmin, xmax, ymin, ymax)

  • define a 3d bar chart with an appropriate function in R that can return the height of each bar and
    xedges, yedges (I called this numpy funtion: https://docs.scipy.org/doc/numpy/reference/generated/numpy.histogram.html)

  • from the data returned by the above function set up an array that holds the 3d position of each 3d bar, i.e. the positions
    in the xOz plane, of z coordinate, z=0, where the above template cube is to be translated.

  • each bar has three numerical caracteristics:

xsize = xedges[1]-xedges[0]-bargap
ysize = yedges[1]-yedges[0]-bargap
height (returned by an appropriate R function)

My bargap was set to 0.05

To get the vertices of each 3d bar, rescale the template cube to get a parallelepiped of dimensions (xsize, ysize, height) and then
translate it (move it) to the corresponding position mentioned above.

Triangulate each such a parallelepiped extracting its vertices and triangular faces.
With a proper R function extract a list of unique vertices and corresponding triangular faces
that define a Plotly Mesh3d.

Here is my Python code https://plot.ly/~empet/15255 and this is a such 3d

bar chart:

4 Likes

Wow. Thank you for such a detailed answer. I certainly did not expect that. Have a nice day :).

@HansPeter123 For a better plot insert flatshading=True in the mes3d definition.

@HansPeter123

Here is another variant of a 3D bar chart for presenting 1D labelled data written in python.