【问题标题】:rgl package - cube within a cubergl 包 - 立方体中的立方体
【发布时间】:2017-12-08 12:49:54
【问题描述】:

我正在尝试在立方体内创建一个矩形棱柱。我需要立方体的尺寸为 1x1x1 单位,原点为 0,0,0。立方体内的矩形理想情况下将从原点开始,然后从矢量变量中拉出以获取其 XYZ 尺寸。矩形棱镜只能具有范围从 0 到 1 的正值,这就是为什么我只想显示正值而不是 cube3d 的默认值,即在原点周围的所有维度上显示 -1 到 1。

有人可以为我指出正确的方向吗?

数据示例:

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)  

我可以调整此功能以满足我的需要吗?如果是这样,该方法会是什么样子?如果这不是正确的功能,您会建议什么?

我发现了另一种可能的方法,但它需要不同的输入,因此不是首选方法,描述为here

【问题讨论】:

    标签: r 3d title axis-labels rgl


    【解决方案1】:

    我不确定我是否能理解您的问题,但也许 scale3d()translate3d() 会满足您的需求(请参阅:?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")
    

    【讨论】:

    • 谢谢!这很好用。当您给我回信时,我刚刚找到了 scale3d 函数,但未能找到使其正常工作的方法。 %>% 也很棒——我也不知道它的存在。绝对可以节省时间/空间!
    • 一旦我得到代码的“最终版本”,我会用它来回应。再次感谢
    • 可以去掉library(dplyr)语句;你从不使用那个包中的任何东西。
    • @user2554330;哇,我不知道rgl 导入管道。谢谢你的建议。
    • 我现在已经发布了我的更新代码。请让我知道您是否还有其他 cmets。感谢您迄今为止的所有帮助。
    【解决方案2】:
     #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)
    

    【讨论】:

    • 您希望标题在哪里?如果您希望它随场景移动,请使用mtext3d;如果不这样做,您可以使用bgplot3d 将其写入后台,或者像layout3d 示例那样将视图拆分为移动和非移动部分。
    • 您好用户 - 感谢您的建议。 bgplot3d 效果很好!现在将此添加到我已完成的答案中。
    • 我不会使用plot.new() + title(...)。它在这里工作(两个函数都返回NULL),但在其他情况下,您可能会因尝试添加无法添加的内容而收到警告或错误。最好使用{ plot.new(); title(...) }(或两行相同的内容,不需要分号)。
    • 另外,我不认为 R 保证 plot.new() 将在总和中首先被评估,但它会在复合语句中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-17
    • 2013-08-09
    相关资源
    最近更新 更多