2016-06-13 2 views
4

R의 3D 플롯에 2D 플롯을 추가 할 수 있습니까?hist3D 2D 플롯을 백그라운드로 R

다음 막대 코드를 생성하는 다음 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

내가에 플롯을 추가 할 수있게되고 무엇을 찾고 있어요 : 이것은 다음과 같은 3 차원 그래프를 생성

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

를 추가합니다. 나는 당신이'hist3D' 함수를 해킹하지 않고'plot3D'로 그것을 할 수 있다고 생각하지 않습니다. 'rgl'에서'plot3d'를 사용하여 3D 플롯을 생성하면'points3d' 또는'lines3d' 또는'add = T'를 사용하여 새로운 2D 플롯의 포인트를 각각의 색상으로 슈퍼 임 포즈 할 수 있습니다. 모든 점에 대해 축 중 하나를 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"))) 


RGL

에 3D barplot 만들기
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``이 할 수있는 2D 플롯

# 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는 바이어야합니다. 목록 맞습니까? –

+0

오, 미안, 내가 잘못했다. 나는 그것을 수정할 것이다. – cuttlefish44

+0

고마워요, 좋은 답변입니다. –

관련 문제