我想用两个3d散点图使用rgl使用knitr
生成一个html文档,但我尝试将图例添加到这两个图时有问题。使用完全相同的代码(从一个块复制并粘贴到另一个块,然后更改要绘制的值),第一个图的图例以HTML方式显示得太大。我已经生成了一个与我正在使用的数据类似的示例脚本:意想不到的行为与plot3d和knitr绘图传说
---
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)}
```
任何帮助将不胜感激!
对我有何意见? –
是的,对不起,@MikeWise。当我用我的数据尝试你的建议时,发生的事情是这两个传说变得太大了。所以我试图找出发生了什么事情,或者至少要创建一个例子,用与我的真实数据更类似的数据复制它。我会很快发布它作为答案! –
因此,您得到的结果与我发布的结果不同? –