简体   繁体   中英

How to produce 3D segmented bar plot with R

How do we produce a 3D 'segmented' bar plot with R? I found a similar post but it was closed many years ago and no working answer has been given till now. I could find a solution with python but would really want to generate it with R.

I understand that some of the bar plots behind will not be clearly presented but I'm not worry about it. Despite the issue of presenting the data properly in 3D, I still wish to generate it and if possible, rotate so that those behind can be observed at different angle.

Can someone help please? Thanks.

Since I couldn't obtain an answer for using R, I attempted with python (solution by other post but with an increased number of stacks) and achieved what I want, where I can rotate and view the bar chart at different angles.

import pandas as pd
import matplotlib.pyplot as plt  
#from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d import axes3d
import numpy as np


# Set plotting style
plt.style.use('seaborn-white')

dz=[]
z0 = np.array([ 1.,  3.,  11.,   8.,   7.,   6.,   6.,   6.,   5.,   4.,
                3.,   11.,   10.,  1.,  1.,  7.])
dz.append(z0)

z1 =[ 5.,   5.,   8.,   4.,   2.,   0.,   0.,   0.,   0.,   0.,   0.,
      1.,   6.,  5.,   7.,   2.]

dz.append(z1)

z2 =[ 15.,   5.,   8.,   2.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,
      3.,   5.,  2.,   7.,   2.]

dz.append(z2)

_zpos = z0*0


xlabels = pd.Index(['X01', 'X02', 'X03', 'X04'], dtype='object')

ylabels = pd.Index(['Y01', 'Y02', 'Y03', 'Y04'], dtype='object')

x = np.arange(xlabels.shape[0])

y = np.arange(ylabels.shape[0])

x_M, y_M = np.meshgrid(x, y, copy=False)

fig = plt.figure(figsize=(7, 7))
ax = fig.add_subplot(111, projection='3d')

# Making the intervals in the axes match with their respective entries
ax.w_xaxis.set_ticks(x + 0.5/2.)
ax.w_yaxis.set_ticks(y + 0.5/2.)

# Renaming the ticks as they were before
ax.w_xaxis.set_ticklabels(xlabels)
ax.w_yaxis.set_ticklabels(ylabels)

# Labeling the 3 dimensions
ax.set_xlabel('X label')
ax.set_ylabel('Y label')
ax.set_zlabel('Z label')

# Choosing the range of values to be extended in the set colormap
values = np.linspace(0.2, 1., x_M.ravel().shape[0])

# Selecting an appropriate colormap

colors = ['#FFC04C', 'blue', '#3e9a19', 
          '#599be5','#bf666f','#a235bf','#848381','#fb90d6','#fb9125']

# Increase the number of segment to 3 by changing the X in 'range(X)' to 3.
for i in range(3):
    ax.bar3d(x_M.ravel(), y_M.ravel(), _zpos, dx=0.3, dy=0.3, dz=dz[i], 
              color=colors[i])
    _zpos += dz[i]
 

#plt.gca().invert_xaxis()
#plt.gca().invert_yaxis()
Segment1_proxy          = plt.Rectangle((0, 0), 1, 1, fc="#FFC04C90")   
Segment2_proxy         = plt.Rectangle((0, 0), 1, 1, fc="blue")

ax.legend([Segment1_proxy,
           Segment2_proxy],['Segment1',
                            'Segment2',
         ])
plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM