向 scatter3d 图添加图例 [英] Adding a legend to scatter3d plot

查看:33
本文介绍了向 scatter3d 图添加图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

交互式 3D 绘图可能的软件包之一是 rgl.我想做的是根据一些因子变量构建带有颜色编码的 3D 散点图.3D 维散点图用于 plsr 分析的结果载荷.

One of the possible packages for interactive 3D plots is rgl. What I would like to do is to build 3D scatterplot with color coding according to some factor variable. The 3D dimensional scatterplot is for resulting loadings from plsr analysis.

结果图看起来像

示例数据在表格中给出:

> loadings

      |      Comp 1         |        Comp 2            |    Comp 3           | Class
-------------------------------------------------------------------------------------------                    
TEMP  | -0.0607044182964255 | "0.0437618450165671"     |"0.045124991801441"  | "global"  
MW    | "-0.13414890573833" |   "-0.0970537799069731"  |0.263043734662182"   | "local" 
DM    |"-0.183751529577861" |  "-0.102703237685933"    |"0.0640549385564205" | "global" 
CHG   |"-0.0558781715833019"| "0.125155347350922"      |"-0.119258450107321" | "local"

或者可以生成:

loadings <- data.frame(Comp1 = c(1.2, 3.4, 5.6, 13.1), Comp2 = c(4.3, 1.2, 7.7, 9.8),
                       Comp3 = c(1.2,6.9,15.6,15.0), 
                       row.names = c("TEMP", "MW", "DM", "CHG"), 
                       Class = c("global", "local", "global", "local"))
scatter3d(x=loadings[[1]], y=loadings[[2]], z=loadings[[3]], 
          point.col = as.numeric(as.factor(loadings[,4])), size = 10)

获得的情节具有相同的风格,但更简单,因为只有两个级别的变量Class":'global'和'local'

Obtained plot has the same style, but is much simpler as there are only two levels of variable "Class": 'global' and 'local'

问题是:是否有可能在 rgl 中添加图例,或者一些独立的图例可以附加到情节中?预先感谢您的帮助!

Question is: Is it any possibility to add legend within rgl or maybe some independent legend can be attached to the plot? Thanks in advance for help!

答案是:

scatter3d(x=loadings[[1]], y=loadings[[2]], z=loadings[[3]], 
          point.col = as.numeric(as.factor(loadings[,4])), size = 10, type = 's')
text3d(x=1.1, y=c(.9,1), z=1.1,levels(loadings[[4]]),col="black")
points3d(x=1.2,y=c(.9,1),z=1.1, col=as.numeric(as.factor(loadings[,4])), size=5)

根据类别带有标签的图:

The plot with labels according to classes:

推荐答案

那不是 plot3d 图像(除非你加载了另一个包),但看起来像一个 scatter3d 使用 John Fox 的 car 包中的 scatter3d 函数构建的渲染:

That's not a plot3d image (unless perhaps you have another package loaded) , but looks like a scatter3d rendering constructed with the scatter3d function from the car package by John Fox:

  require(rgl)
  require(car)
  scatter3d(x=loadings[[1]], y=loadings[[2]], z=loadings[[3]], 
            point.col = as.numeric(as.factor(loadings[,4])), size = 10)

scatter3d 函数确实依赖于 rgl 函数,但有很多自定义选项.您没有提供构建图例"的代码,所以我使用了 rgl::text3d 中提供的示例:

The scatter3d function does depend on rgl functions but has quite a few customization options. You offered no code to construct a "legend", so I played around with the example offered in rgl::text3d:

 text3d(1+ font/8, 1+cex/4, 1+famnum/8, text=paste(family, font), adj = 0.5, 
       color="blue", family=family, font=font, cex=cex)

使用新数据,这是对文本和点数请求的响应:

With the new data this is in response to the request for text and points:

 scatter3d(x=loadings[[1]], y=loadings[[2]], z=loadings[[3]], 
             point.col = as.numeric(as.factor(loadings[,4])), size = 10)
 text3d(x=1.1, y=c(.9,1,1.1), z=1.1, names(loadings) ,col="black")
 points3d(x=1.2,y=c(.9,1,1.1),z=1.1, col=as.numeric(as.factor(loadings[,4])), size=5)

这篇关于向 scatter3d 图添加图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆