如何在循环下的markdown(knitr)中包含多个plot3d [英] How to include multiple plot3d in markdown (knitr) under a loop

查看:48
本文介绍了如何在循环下的markdown(knitr)中包含多个plot3d的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在循环中使用plot3d {rgl}绘制3d图. knitr rgl挂钩对于单个3d图形效果很好,但是在循环中使用时,markdown文件不包含3d图形.

I am plotting a 3d graph using plot3d {rgl} in a loop. knitr rgl hooks works fine for single 3d graph but when used in a loop, markdown file doesn't include 3d graphs.

推荐答案

一种方法是使用mfrow3d()在单个场景"中放置多个子场景",然后使用rglwidget()命令执行在您的html中显示场景.查看更多信息此处 .以下是 Yihui的答案中的脚本的大量修改版本:

One way you can do it is to use mfrow3d() to put multiple "subscenes" in a single "scene", then use the rglwidget() command to show the scene in your html. See more information here. Below is a heavily modified version of the script from 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)
```

这篇关于如何在循环下的markdown(knitr)中包含多个plot3d的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆