我正在尝试使用knitr
使用rgl生成带有两个三维散点图的html文档,但是当我尝试在两个图中添加图例时遇到问题。使用完全相同的代码(从一个块复制并粘贴到另一个块,然后将值更改为绘图),第一个图的图例以html方式显示得太大。我已经生成了一个示例脚本,其中包含与我正在使用的数据类似的数据:
---
title: "example"
output: html_document
---
Example script
# First plot
```{r 1st chunk, webgl=TRUE, echo = FALSE, fig.width=10, fig.height=10}
library(rgl)
knitr::knit_hooks$set(webgl = hook_webgl)
dt <- data.frame(x = runif(70, min = 1, max = 10),
y = runif(70, min = -10, max = 1),
z = runif(70, min = -20, max = -10),
groups = rep(c("Group1", "Group2", "Group3", "Group4", "Group5", "Group6", "Group7"), each = 10))
plot3d(dt$x, dt$y, dt$z,
xlab = "X", ylab = "Y", zlab = "Z",
type ="n")
grid3d(c("x", "y+", "z"))
legend3d("bottomright",
c("Group1", "Group2", "Group3", "Group4", "Group5", "Group6", "Group7"),
col = c(1:7),
pch = 20,
bg = "black",
bty = "n",
y.intersp = 0.8,
cex = 1.5)
for (i in seq_along(unique(dt$groups))){
sub <- dt[dt$groups == unique(dt$groups)[i],]
pch3d(sub$x, sub$y, sub$z,
col = i,
pch = 16,
radius = 0.2,
add = T)}
```
This first legend is way too large, while, with the exame same code, for the next plot, the legend is normal size
# Second plot
```{r 2nd chunk, webgl=TRUE, echo = FALSE, fig.width=10, fig.height=10}
library(rgl)
knitr::knit_hooks$set(webgl = hook_webgl)
dt <- data.frame(x = runif(150, min = 2, max = 20),
y = runif(150, min = -20, max = 2),
z = runif(150, min = -30, max = -20),
groups = rep(c("Group1", "Group2", "Group3", "Group4", "Group5", "Group6", "Group7", "Group8",
"Group9", "Group10", "Group11", "Group12", "Group13", "Group14", "Group15"), each = 10))
plot3d(dt$x, dt$y, dt$z,
xlab = "X", ylab = "Y", zlab = "Z",
type ="n")
grid3d(c("x", "y+", "z"))
legend3d("bottomright",
c("Group1", "Group2", "Group3", "Group4", "Group5", "Group6", "Group7", "Group8",
"Group9", "Group10", "Group11", "Group12", "Group13", "Group14", "Group15"),
col = c(1:15),
pch = 20,
bg = "black",
bty = "n",
y.intersp = 0.8,
cex = 1.5)
for (i in seq_along(unique(dt$groups))){
sub <- dt[dt$groups == unique(dt$groups)[i],]
pch3d(sub$x, sub$y, sub$z,
col = i,
pch = 16,
radius = 0.2,
add = T)}
```
任何帮助将不胜感激!
答案 0 :(得分:1)
有点奇怪,我看着它,注意到cex
调用中的legend3d
参数(这意味着“字符扩展”)在调用call时以某种方式被不同地解释。看起来像一个小错误。
但是我也发现你可以通过在每个情节之前添加open3d
命令来修复它。也许这是一个修复?
代码:
---
title: "example"
output: html_document
---
Example script
```{r 0th chunk, webgl=TRUE, echo = FALSE}
library(rgl)
knitr::knit_hooks$set(webgl = hook_webgl)
```
# First plot
```{r 1st chunk, webgl=TRUE, echo = FALSE, fig.width=5, fig.height=5}
glob_cex <- 0.6
open3d()
dt <- data.frame(x = runif(70, min = 1, max = 10),
y = runif(70, min = -10, max = 1),
z = runif(70, min = -20, max = -10),
groups = rep(c("Group1", "Group2", "Group3", "Group4", "Group5", "Group6", "Group7"), each = 10))
plot3d(dt$x, dt$y, dt$z,
xlab = "X", ylab = "Y", zlab = "Z",
type ="n")
grid3d(c("x", "y+", "z"))
legend3d("bottomright",
c("Group1", "Group2", "Group3", "Group4", "Group5", "Group6", "Group7"),
col = c(1:7),
pch = 20,
bg = "black",
bty = "n",
y.intersp = 0.8,
cex = 0.8 )
for (i in seq_along(unique(dt$groups))){
sub <- dt[dt$groups == unique(dt$groups)[i],]
pch3d(sub$x, sub$y, sub$z,
col = i,
pch = 16,
radius = 0.2,
add = T)}
```
Legends are now the same size by adding an open3d() command before each plot.
# Second plot
```{r 2nd chunk, webgl=TRUE, echo = FALSE, fig.width=5, fig.height=5}
open3d()
dt <- data.frame(x = runif(150, min = 2, max = 20),
y = runif(150, min = -20, max = 2),
z = runif(150, min = -30, max = -20),
groups = rep(c("Group1", "Group2", "Group3", "Group4", "Group5", "Group6", "Group7", "Group8",
"Group9", "Group10", "Group11", "Group12", "Group13", "Group14", "Group15"), each = 10))
plot3d(dt$x, dt$y, dt$z,
xlab = "X", ylab = "Y", zlab = "Z",
type ="n")
grid3d(c("x", "y+", "z"))
legend3d("bottomright",
c("Group1", "Group2", "Group3", "Group4", "Group5", "Group6", "Group7", "Group8",
"Group9", "Group10", "Group11", "Group12", "Group13", "Group14", "Group15"),
col = c(1:15),
pch = 20,
bg = "black",
bty = "n",
y.intersp = 0.8,
cex = 0.8 )
for (i in seq_along(unique(dt$groups))){
sub <- dt[dt$groups == unique(dt$groups)[i],]
pch3d(sub$x, sub$y, sub$z,
col = i,
pch = 16,
radius = 0.2,
add = T)}
```
屏幕截图:
答案 1 :(得分:0)
我建议你不要使用webgl=TRUE
,在底部加rglwidget()
电话。这使得文本太大,因为它从小的256x256 rgl窗口放大了;使用r3dDefaults$windowRect <- c(0,0,1000,1000)
(或类似的大值)来获得更好的大小。
您可能还想在开头使用options(rgl.useNULL = TRUE)
,因此您不会在编织时弹出窗口。这样做的副作用是允许您将r3dDefaults$windowRect
值设置为大于物理屏幕的值。
r3dDefaults$windowRect
和options("rgl.useNULL")
值只需在文档顶部附近设置一次,除非您明确更改,否则将应用于以下所有绘图。