【问题标题】:How to include multiple plot3d in markdown (knitr) under a loop如何在循环下的markdown(knitr)中包含多个plot3d
【发布时间】:2016-07-29 17:31:10
【问题描述】:

我正在循环使用 plot3d {rgl} 绘制 3d 图形。 knitr rgl hooks 适用于单个 3d 图形,但在循环中使用时,markdown 文件不包含 3d 图形。

【问题讨论】:

    标签: r knitr


    【解决方案1】:

    您可以这样做的一种方法是使用mfrow3d() 将多个“子场景”放在一个“场景”中,然后使用rglwidget() 命令在您的html 中显示场景。查看更多信息here。以下是来自Yihui's answer 的脚本的大量修改版本:

    ---
    output: html_document
    ---
    
    You will need to install the rglwidget library if you have not already.
    ```{r setup}
    library(knitr)
    library(rgl)
    library(rglwidget)
    ```
    
    Using mfrow3d() I create a scene with 5 subscenes,
    so each of the 5 plots will appear in a single scene.
    Each will still be independently interactive (can zoom in on one at a time, e.g.).
    
    ```{r testgl}
    x <- sort(rnorm(1000))
    y <- rnorm(1000)
    z <- rnorm(1000) + atan2(x,y)
    
    mfrow3d(nr = 5, nc = 1)
    cols <- c("red", "orange", "green", "blue", "purple")
    for(i in 1:5){
        plot3d(x, y, z, col = cols[i])
    }
    rglwidget(height = 4000, width = 1000)
    ```
    

    【讨论】:

      【解决方案2】:

      另一种解决方案是包含多个rglwidget() 值。您需要将它们全部放在对htmltools::tagList() 的调用中才能正常工作。 (如果您使用来自https://r-forge.r-project.org/R/?group_id=234rgl 的开发版本,则不需要rglwidget 包)。修改文斯的例子,

      ---
      output: html_document
      ---
      
      ```{r setup}
      library(rgl)
      library(rglwidget)
      ```
      
      ```{r testgl}
      x <- sort(rnorm(1000))
      y <- rnorm(1000)
      z <- rnorm(1000) + atan2(x,y)
      
      cols <- c("red", "orange", "green", "blue", "purple")
      result <- list()
      for(i in 1:5){
          plot3d(x, y, z, col = cols[i])
          result[[i]] <- rglwidget()
      }
      htmltools::tagList(result)
      ```
      

      【讨论】:

        猜你喜欢
        • 2022-01-21
        • 2016-07-22
        • 2011-12-07
        • 1970-01-01
        • 1970-01-01
        • 2013-07-01
        • 2022-08-19
        • 1970-01-01
        • 2020-09-02
        相关资源
        最近更新 更多