2017-12-08 18 views
2

私はキューブ内で直角プリズムを作成しようとしています。キューブは、1x1x1単位の寸法を持ち、起点は0,0,0である必要があります。立方体内の矩形は、理想的には原点から始まり、次にベクトル変数から引っ張ってXYZ寸法を取得します。直角プリズムの正の値は0から1までしかないので、原点を中心にすべての次元で-1:1を示すcube3dのデフォルトのように見えるのではなく、正の値を表示する必要があります。rglパッケージ - キューブ内のキューブ

この作業を行う方法について、誰かが正しい方向で私を指摘できますか?

データ例:

Augusta = c(0.4, 0.2, 0.8) 

コード私は現在、(stackoverflowのからプル)持っている -

c3d <- cube3d(color="red", alpha=0.5) 
c3d 
shade3d(c3d) 
axes3d() 
rgl.viewpoint(theta = 45, phi = 25, fov = 60, zoom = 1) 

私は自分のニーズに合わせて、この機能を適応させることはできますか?もしそうなら、その方法はどのように見えますか?これが正しい機能でない場合は、何を提案しますか?

異なる入力を必要とするため、好まれないであろう私が見つけた別の潜在的な方法は、しかし、私は私はあなたの問題を理解できるかどうかわからないんだけど、おそらくscale3d()translate3d()与えるだろうhere

答えて

0
#Data example 
Nominal_City_Name = c(0.7,0.2,0.5) 
X = Nominal_City_Name[1] 
Y = Nominal_City_Name[2] 
Z = Nominal_City_Name[3] 

#Bring in RGL library 
library(rgl) 

#Contributor cuttlefish44's code 
c3d <- cube3d(color="grey", alpha=0.1) 
c3d2 <- c3d %>% 
    translate3d(1, 1, 1) %>% 
    scale3d(0.5, 0.5, 0.5) 

c3d3 <- cube3d(color = "blue", alpha = (0.5)) %>% 
    translate3d(1, 1, 1) %>% 
    scale3d(0.5, 0.5, 0.5) %>% 
    scale3d(X, Z, Y) 

shade3d(c3d2) 
shade3d(c3d3) 
axes3d() 
# Add points at vertices 
points3d(t(c3d3$vb), size = 5) 
# Add lines to edges of box 
for (i in 1:6) lines3d(t(c3d3$vb)[c3d3$ib[,i],]) 

#------------Add labels/title to 3d window------- 
# This version of title (commented out) doesn't work as well as the 
# bgplot3d() version now included in output section below. 
# Use this title3d() version if you want the title to be dynamic to 
# the graphic.  

#Title_XYZ = paste0(stakeholder," ","X, Y, Z") 
#title3d(main =Title_XYZ,cex = 2, line = 2) 

mtext3d("X Var",edge="x-+",line=2,las=2,cex=2,srt = 50,col = 
      "darkorange3") 
mtext3d("Y Var",edge="z+-",line=2.5,las=2,cex=2, col = 
      "chartreuse4", srt = 90) 
mtext3d("Z Var",edge="y-+",line=3,las=2,cex=2, col = 
      "darkblue") 

# 
#-------Create output file------- 
#This section first sets the window view parameters and window size 
# to what I want it to be. Then it exports to a location you choose. 

# After dynamically moving it to look the way you want in 3d view - 
# Use par3d() to get view attributes (i.e., windowRect (window size) 
# info), among other measurements. Theta, phi, fov, and zoom give 
# angles, field of vision, and zoom. 

rgl.viewpoint(theta = 45, phi = 4, fov = 60, zoom = 1) 
window_size = c(164,32,1259,1050) 
par3d(windowRect = window_size) 

#Adding title using a background plot. This must be done AFTER 
#resizing the window, because it doesn't scale gracefully. 
Title_XYZ = "This is your title" 
bgplot3d(
    plot.new() + 
    title(main = Title_XYZ, line = -10,cex.main=3)) 

     #a = folder, b = stakeholder name, c = file extension, d = concat  of 
    #all 3 for export 
    # a = "C:\\Users\\MyUserName\\Documents\\R\\export" 
    # b = Title_XYZ 
    # c=".jpg" 
    # d = paste0(a,b,c) 
    # rgl.snapshot(d) 
+0

タイトルはどこにありますか?シーンと一緒に動かすには 'mtext3d'を使います。もしそうでなければ、 'bgplot3d'を使ってバックグラウンドに書き込んだり、viewを' layout3d'のように動く部分と動かない部分に分割します。 – user2554330

+0

こんにちはユーザー - ご提案いただきありがとうございます。 bgplot3dはうまくいった!これを完成した回答に今追加する。 – jrcook1106

+0

'plot.new()+ title(...)'は使用しません。ここでは(両方の関数が 'NULL'を返す)動作しますが、他の場合には、追加できないものを追加しようとすると警告またはエラーが発生する可能性があります。 '{plot.new(); title(...)} '(または2行で同じもの、セミコロンは必要ありません)。 – user2554330

1

に記載されていますあなたが望むもの(?scale3d参照)。

library(rgl) 

c3d <- cube3d(color="red", alpha=0.5) 
c3d2 <- c3d %>% 
    translate3d(1, 1, 1) %>% 
    scale3d(0.5, 0.5, 0.5) 

c3d3 <- cube3d(color = "blue") %>% 
    translate3d(1, 1, 1) %>% 
    scale3d(0.5, 0.5, 0.5) %>% 
    scale3d(0.4, 0.2, 0.8) 

shade3d(c3d2) 
shade3d(c3d3) 
axes3d() 
# title3d(xlab = "x", ylab = "y", zlab = "z") 

enter image description here

+0

ありがとう!これは素晴らしいです。あなたが私を書いて私はscale3d関数を見つけただけですが、それを正しく動作させる方法を見つけることができませんでした。 %>%も素晴らしいです - 私はどちらか存在していたのか分かりませんでした。間違いなく時間/空間の節約! – jrcook1106

+0

コードの「最終版」になったら、私はそれに応答します。再度ありがとう – jrcook1106

+0

'library(dplyr)'ステートメントを取り除くことができます。あなたはそのパッケージから何も使用しません。 – user2554330