英文:
Color of text and ellipses not working in rgl
问题
我有一个在rgl
中的简单的3D散点图,我想要在图上添加数据椭球和文本标签,但是颜色无法工作。椭球显示为灰色,文本标签显示为白色。
此外,我如何去除坐标轴上的刻度标记?
英文:
I have a simple 3D scatter plot in rgl
where I want to add data ellipsoids and text labels, but I can't get colors to work. The ellipsoids appear in grey, and the text labels appear in white.
#' ---
#' title: Penguins data with 3D ellipsoids
#' ---
library(ggplot2)
library(dplyr)
library(tidyr)
library(rgl)
data(penguins, package = "palmerpenguins")
penguins <- penguins |>
drop_na()
#' Select variables
peng <- penguins |>
select(species, starts_with("bill"), body_mass_g) |>
rename_with(~ stringr::str_remove(., '_mm$|_g$'))
str(peng)
shapes <- c(15, 17, 18)
shapes <- 1:3
colors <- rainbow(3)
colMax <- function(data) sapply(data, max, na.rm = TRUE)
open3d()
par3d(windowRect = c(0, 0, 800, 800) + 50)
rgl.bringtotop()
plot3d(peng[,-1], type = "n")
pch3d(peng[,-1],
pch = shapes,
col = adjustcolor(colors[peng$species], alpha=0.4),
cex = 0.1,
decorate = FALSE)
offset <- 0.01
for (sp in levels(peng$species)) {
xyz <- subset(peng, species == sp)[, 2:4]
# ellipsoids
mu <- apply(xyz, 2, mean)
sigma <- cov(xyz)
ell <- ellipse3d(sigma, centre = mu, level = 0.68)
shade3d(ell,
alpha = 0.2, color = colors[sp])
# find location to label the ellipse
max <- colMax(xyz)
bbox <- matrix(rgl::par3d("bbox"), nrow=2)
ranges <- apply(bbox, 2, diff)
texts3d(max, adj = 0, text = sp, color = colors[sp], cex = 3)
}
Also, how can I remove the tick marks on the axes?
答案1
得分: 3
你忘了:
names(colors) <- levels(peng$species)
所以你有一些NA
颜色。
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论