scatter3d - 基于第四个变量的颜色点 [英] scatter3d - colour dots based on 4th variable

查看:33
本文介绍了scatter3d - 基于第四个变量的颜色点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 scatter3d 绘制 3D 绘图,但不知道如何根据第四个变量(此处为 VAR4)的值为点着色.有人可以帮助我吗?如果通过添加这些颜色,我仍然保留默认版本中的褪色效果(3D 图中更远的点显示为较浅的颜色),那就太好了.谢谢!

I'd like to do a 3D plot with scatter3d but don't know how to colour the dots based on the values of a fourth variable (here VAR4). Could someone kindly help me? It would be great if by adding these colours, I still keep the fading effect that is in the default version (the points the further in the 3D plot appear with a lighter colour). Thank you!

df <- data.frame(VAR4=c(10,52,78,34,13,54), 
                   A=c(12, 8, 10, 7, 13, 15), 
                   B=c(4,3,2,1,7,5), 
                   C=c(1,3,2,1,3,1))

library(rgl)
library(car)
scatter3d(A ~B + C,color=VAR4, data=df,  surface=F)

推荐答案

你可以试试这个:

library(rgl)
library(car)
# add as group the VAR4, making as factor
scatter3d(A ~B + C, groups = as.factor(df$VAR4), data=df,  surface=F)

但是,如果我能给你建议的话,使用 plotly 包会更漂亮更好:

But, if I could advice you, it's more pretty and nice to use the plotly package:

library(plotly)
# in this case we use VAR4 as continuous, you can put color = ~as.factor(VAR4) to have it as factors
plot_ly(df, x = ~A, y = ~B, z = ~C, color = ~VAR4) %>%
  add_markers() %>%
  layout(scene = list(xaxis = list(title = 'A'),
                      yaxis = list(title = 'B'),
                      zaxis = list(title = 'C')))

这篇关于scatter3d - 基于第四个变量的颜色点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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