2016-06-13 19 views
4

Rの3Dプロットに2Dプロットを追加することは可能ですか?hist3D 2DプロットのバックグラウンドR

私は以下の3次元バープロットを生成するRコードを持っています。唯一

dt = structure(c(1, 1, 1, 3, 
       0, 2, 2, 1, 
       1, 2, 1, 3, 
       2, 6, 3, 1, 
       1, 2, 3, 0, 
       1, 0, 2, 1, 
       1,2,2,2), .Dim = c(4L, 7L), .Dimnames = list(c("0-50", 
                    "51-60", "61-70", "71-80" 
       ), c("0-50", "51-60", "61-70", "71-80", "81-90", "91-100", "101-Inf"))) 

m <- matrix(rep(seq(4),each=7), ncol=7, nrow=4, byrow = TRUE) 

hist3D(x = 1:4, z = dt, scale = T,bty="g", phi=35,theta=30,border="black",space=0.15,col = jet.col(5, alpha = 0.8), add = F, colvar = m, colkey = F, ticktype = "detailed") 

hist3dコール:

enter image description here

は、私はプロットを追加できることです探しています何を:これは、次の3Dプロットを生成

hist3D(x = 1:4, z = dt, scale = T,bty="g", phi=35,theta=30, border="black", space=0.15,col = jet.col(5, alpha = 0.8), add = F, colvar = m, colkey = F, ticktype = "detailed") 

灰色のグリッドがある位置。出来ますか?

ありがとうございます!

+0

2Dプロットを追加します。私はあなたが 'hist3D'関数をハックすることなく' plot3D'でそれを行うことはできないと思います。 'rgl'でそれを行うには、' plot3d'を使って3Dプロットを作成してから、 'points3d'や' lines3d'や 'add = T'を使って新しい2Dプロットの点をスーパーインポーズすることができます。すべての点で軸の1つを0に設定するだけで2Dになります。 –

+0

@ Hack-Rヒントありがとうございます。あなたが言ったように私は試みます。 –

答えて

2

私が知る限り、RGLにはバープロットを作る良い機能はありません。私は手動の方法を提案する。

dt = structure(c(1, 1, 1, 3, 
       0, 2, 2, 1, 
       1, 2, 1, 3, 
       2, 6, 3, 1, 
       1, 2, 3, 0, 
       1, 0, 2, 1, 
       1,2,2,2), .Dim = c(4L, 7L), .Dimnames = list(c("0-50", 
                   "51-60", "61-70", "71-80" 
       ), c("0-50", "51-60", "61-70", "71-80", "81-90", "91-100", "101-Inf"))) 


library(rgl) 

# make dt xyz coordinates data 
dt2 <- cbind(expand.grid(x = 1:4, y = 1:7), z = c(dt)) 
# define each bar's width and depth 
bar_w <- 1 * 0.85 
bar_d <- 1 * 0.85 
# make a base bar (center of undersurface is c(0,0,0), width = bar_w, depth = bar_d, height = 1) 
base <- translate3d(scale3d(cube3d(), bar_w/2, bar_d/2, 1/2), 0, 0, 1/2) 
# make each bar data and integrate them 
bar.list <- shapelist3d(
    apply(dt2, 1, function(a) translate3d(scale3d(base, 1, 1, a[3]), a[1], a[2], 0)), 
    plot=F) 
# set colors 
for(i in seq_len(nrow(dt2))) bar.list[[i]]$material$col <- rep(jet.col(5)[c(1:3,5)], 7)[i] 

open3d() 
plot3d(0,0,0, type="n", xlab="x", ylab="y", zlab="z", box=F, 
     xlim=c(0.5, 4.5), ylim=c(0.5, 7.5), zlim=c(0, 6.2), aspect=T, expand=1) 
shade3d(bar.list, alpha=0.8) 
wire3d(bar.list, col="black") 
light3d(ambient="black", diffuse="gray30", specular="black") # light up a little 


RGL

で3D barplotを作るあなたはrgl` `でこれを行うことができ

# show2d() uses 2d plot function's output as a texture 
# If you need the same coordinates of 2d and 3d, plot3d(expand=1) and show2d(expand=1), 
# the same xlims, equivalent plot3d(zlim) to 2d plot's ylim, 2d plot(xaxs="i", yaxs="i") are needed. 
show2d({ 
    par(mar = c(0,0,0,0)) 
    barplot(c(3,4,5,6), yaxs="i", ylim=c(0, 6.2)) 
}, 
expand = 1 , face = "y+", texmipmap = F) # texmipmap=F makes tone clear 

enter image description here

+0

偉大な答えをありがとう!しかし、もし私が正しいかどうかは分かりませんが、obj.listはbar.listのようにする必要がありますか? –

+0

ああ、申し訳ありませんが、間違いました。私はそれを変更します。 – cuttlefish44

+0

ありがとう、良い答え。 –