使用rgl包在交互式3D图中添加固定标题,R

10
我将尝试使用R的rgl包来为交互式3D图形添加一个固定的标题,但目前还没有成功。我还想在主标题下方添加一个副标题。
以下是示例代码:
library(rgl)

data<-read.table(text="          X         Y         Z
                 1  147.0883 -18.69122 -13.90000
                 2  147.0894 -18.69455 -10.97250
                 3  147.0883 -18.69122 -17.45000
                 4  147.0883 -18.69122 -15.44000
                 5  147.0883 -18.69122 -13.45000
                 6  147.0909 -18.69922 -12.25000
                 7  147.0883 -18.69122 -17.30000
                 8  147.0883 -18.69122 -16.40000
                 9  147.0883 -18.69122 -14.30000
                 10 147.0883 -18.69122 -18.50000
                 11 147.0883 -18.69122 -15.67606
                 12 147.0883 -18.69122 -17.25780
                 13 147.0883 -18.69122  -3.64000
                 14 147.1164 -18.68133 -22.13000
                 15 147.0883 -18.69122 -18.54778
                 16 147.0883 -18.69122 -15.50000
                 17 147.1185 -18.68691 -14.55500
                 18 147.0883 -18.69122 -18.12500
                 19 147.0901 -18.69670 -14.39767",header=T)

data

a<-as.matrix(data)

# call the plug-in bandwidth estimator 
H.pi<-Hpi(a,binned=TRUE)*3 ## a is a matrix of x,y,z points 

# calculate the kernel densities 
fhat<-kde(a,H=H.pi)

# produce a 3D plot
open3d()

title<-"Individual 1 (male)" # main title
title2<-"Site A" # subtitle

plot(fhat,cont=c(50,95),colors=c("green","red"),drawpoints=TRUE,
     xlab="",ylab="", zlab="",size=1.5,ptcol="black", 
     axes=F,box=T,aspect=c(1,1,0.5),zlim=c(-40,0))

axes3d(c("x--","y--","z-+"),cex=0.8)
mtext3d("Longitude",edge="x--",line=3,las=2,cex=1.1)
mtext3d("Latitude",edge="y--",line=3,las=2,cex=1.1)
mtext3d("Depth (m)",edge="z-+",line=3,las=2,cex=1.1)

title3d(main=title,sub=title2,line=4)

我还没有找到一个好的方法,可以使我的标题(和子标题)固定不动,这样当我将我的图表导出成为3D电影时,主标题不会随着图表旋转。

movie3d(spin3d(axis=c(0,0,1), rpm=4),dir="~/Dropbox/R/Sample plots", duration=15, fps=10, movie="Plot 1")

如果你有一个好的/简单的方法将主标题放在固定位置(可能在下面添加一个副标题),那将会非常棒。提前感谢。

我认为你想要的在 rgl 中不可能实现。你可以考虑在使用其他软件创建电影后再添加标题。 - Bryan Hanson
4个回答

16

现在可以在 RGL 绘图中添加标题,这得益于去年更新的一个功能。

这个想法是使用新的函数 bgplot3d,它会将图输出到 RGL 窗口的背景上,就像普通的绘图一样。

例如:

# A minimal plot3d example
library(rgl)
a = matrix(runif(3000), ncol=3)
plot3d(a, col = rainbow(100)[cut(a[,1], breaks = seq(0,1,le=101), include.lowest = T)])

bgplot3d({
  plot.new()
  title(main = 'This is the main title', line = 3)
  mtext(side = 1, 'This is a subtitle', line = 4)
  # use here any other way you fancy to write your title
})

这将得到:

                                                带有plot3d和bgplot3d的RGL标题

然而,有一个非常重要的警告,在bgplot3d的帮助页面中作为注释说明:

由于背景图绘制为位图,它们不能很好地调整大小。最好先调整窗口大小,然后以该大小绘制背景。


2
目前rgl无法实现此功能。

有没有一种方法可以在绘图移动时将至少主标题固定在背景中的位置? - user1626688
2
不 - ?rgl.texts 表示该文本在三维空间中,面向用户。rgl 图形设备不使用标准的 R 绘图系统,所以 text 不起作用,title 也不起作用等等。我认为只需要在 C++ 层面上进行一些工作,使用 rgl 用于文本的 ftgl 库实现二维静态文本即可。 - Spacedman

1

类似于测试建议,您可以添加一个保持静止的图例,并将其用作标题。请参见以下示例,来自向rgl 3d绘图添加图例

legend3d("topright", legend = paste('Type', c('A', 'B', 'C')), pch = 16, col = rainbow(3), cex=1, inset=c(0.02))

1
要复制您的脚本,需要kde(一个未包含的库)...无论如何,您是否考虑在电影后添加:
text3d(x=0.08, y=0.97, z=0.03, title.main ,col="Blue")

其中:x、y、z是您图形中的位置。我已经与scatter3d一起使用过它(同时也使用了rgl)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接