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

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

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.

  1. #' ---
  2. #' title: Penguins data with 3D ellipsoids
  3. #' ---
  4. library(ggplot2)
  5. library(dplyr)
  6. library(tidyr)
  7. library(rgl)
  8. data(penguins, package = "palmerpenguins")
  9. penguins <- penguins |>
  10. drop_na()
  11. #' Select variables
  12. peng <- penguins |>
  13. select(species, starts_with("bill"), body_mass_g) |>
  14. rename_with(~ stringr::str_remove(., '_mm$|_g$'))
  15. str(peng)
  16. shapes <- c(15, 17, 18)
  17. shapes <- 1:3
  18. colors <- rainbow(3)
  19. colMax <- function(data) sapply(data, max, na.rm = TRUE)
  20. open3d()
  21. par3d(windowRect = c(0, 0, 800, 800) + 50)
  22. rgl.bringtotop()
  23. plot3d(peng[,-1], type = "n")
  24. pch3d(peng[,-1],
  25. pch = shapes,
  26. col = adjustcolor(colors[peng$species], alpha=0.4),
  27. cex = 0.1,
  28. decorate = FALSE)
  29. offset <- 0.01
  30. for (sp in levels(peng$species)) {
  31. xyz <- subset(peng, species == sp)[, 2:4]
  32. # ellipsoids
  33. mu <- apply(xyz, 2, mean)
  34. sigma <- cov(xyz)
  35. ell <- ellipse3d(sigma, centre = mu, level = 0.68)
  36. shade3d(ell,
  37. alpha = 0.2, color = colors[sp])
  38. # find location to label the ellipse
  39. max <- colMax(xyz)
  40. bbox <- matrix(rgl::par3d("bbox"), nrow=2)
  41. ranges <- apply(bbox, 2, diff)
  42. texts3d(max, adj = 0, text = sp, color = colors[sp], cex = 3)
  43. }

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

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

答案1

得分: 3

你忘了:

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

所以你有一些NA颜色。

英文:

You forgot:

  1. 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:

确定