文本和椭圆的颜色在rgl中不起作用。

huangapple go评论99阅读模式
英文:

Color of text and ellipses not working in rgl

问题

我有一个在rgl中的简单的3D散点图,我想要在图上添加数据椭球和文本标签,但是颜色无法工作。椭球显示为灰色,文本标签显示为白色。

这是我得到的效果:
文本和椭圆的颜色在rgl中不起作用。

此外,我如何去除坐标轴上的刻度标记?

英文:

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)
}

Here's what I get:
文本和椭圆的颜色在rgl中不起作用。

Also, how can I remove the tick marks on the axes?

答案1

得分: 3

你忘了:

names(colors) <- levels(peng$species)

所以你有一些NA颜色。

英文:

You forgot:

names(colors) <- levels(peng$species)

So you have some NA colors.

文本和椭圆的颜色在rgl中不起作用。

huangapple
  • 本文由 发表于 2023年5月28日 04:17:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76348867.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定