如何在R中进行3D条形图

时间:2015-03-12 13:42:30

标签: r plot 3d

我想制作这种图表:

3D bar plot

但是,我不知道怎么用R来做。我想知道是否有人知道在R中做到这一点的解决方案?

1 个答案:

答案 0 :(得分:2)

我会使用包rgl。

library(rgl)

# load your data
X= c(1:6)
Y=seq(10,70, 10)
Z=c(-70, -50, -30, -20, -10, 10)

# create an empty plot with the good dimensions
plot3d(1,1,1, type='n', xlim=c(min(X),max(X)), 
                        ylim=c(min(Y),max(Y)), 
                        zlim=c(min(Z),max(Z)), 
                        xlab="", ylab="", zlab="", axe=F )

# draw your Y bars
for(i in X){ segments3d(x = rep(X[i],2), y = c(0,Y[i]), z=0, lwd=6, col="purple")}

# do the same for the Z bars
plot3d(X,0,Z, add=T, axe=F, typ="n")
for(i in X){segments3d(x = rep(X[i],2), y = 0, z=  c(0,Z[i]), lwd=6, col="blue" )}

# draw your axis
axes3d()
mtext3d(text = "Time (days)", edge = "y+", line =3, col=1 )
mtext3d(text = "Change %", edge = "z++", line = 5, col=1 )

view plot

但是我发现条形的宽度限制为6.这可能是一个限制。当你有更多数据时,更好看。

希望它可以提供帮助。