简体   繁体   中英

How to do 3D bar plot in R

I would like to produce this kind of graph:

3D条形图

However, I don't know how to do it using R. I was wondering if someone knew a solution to do it in R?

I would use the package 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 )

查看情节

However I have found the width of the bars restricted to 6. That could be a limit. Better looking when you have more data.

Hope it could help.

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