带有 R 和 Rgl 的彩色球体的 3d 散点图 [英] 3d scatterplot with colored spheres with R and Rgl
问题描述
我想创建一个球体的 3d 散点图,它们的颜色是第四维.我有一个 csv 文件中的数据,其中每行表示粒子的 x、y、z 位置,我有一列告诉我粒子的值(1,2 或 3).如果它们的值为 1 或其他颜色,我想用一种颜色为球着色.
I want to create a 3d scatter plot of spheres with their color being the fourth dimension. I have the data in a csv file where each line indicates the x,y,z position of a particle and I have a column which tells me the value of the particle (1,2 or 3). I want to color the balls in one color if their value is 1 or in another color otherwise.
我创建了以下代码:
library(rgl)
m <- read.csv(file="mem0.csv", sep = ",", head=TRUE)
mcol = m$val
i = 1
mdim = dim(m)
while (i <= mdim[1] ){
if (mcol[i] == 1){
mcol[i] = "red"
}else {
mcol[i] = "blue"
}
i = i +1
}
plot3d(m$x, m$y, m$z, col = mcol, type='s', size=0.1)
编辑编号 2:
我使用 rgl.snapshot() 导出到 svg 文件:
Edit number 2:
I use the rgl.snapshot() to export to an svg file:
数据应该显示一层红球、4层蓝球和一层红球.
The data should display a layer of red balls, 4 layers of blue balls and a layer of red balls again.
推荐答案
rgl
包的 plot3d()
函数可以很容易地完成这样的事情.你甚至可以交互式地探索你的情节:
The plot3d()
function of the rgl
package allows to do such a thing quite easily. And you can even explore your plot interactively :
R> library(rgl)
R> df <- data.frame(x=runif(10,0,1),
+ y=runif(10,0,1),
+ z=runif(10,0,1),
+ color=round(runif(10,1,3)))
R> df
x y z color
1 0.73518229 0.1385970 0.69053482 2
2 0.88789302 0.6872121 0.54734176 2
3 0.79402546 0.5771570 0.89613292 1
4 0.19922140 0.2117405 0.25116078 1
5 0.31825325 0.7449661 0.01174593 2
6 0.64614521 0.4704698 0.68905621 1
7 0.15242295 0.6461338 0.77896858 1
8 0.32698024 0.4548752 0.33969754 3
9 0.00793849 0.6557488 0.75901935 2
10 0.20460232 0.9302882 0.23413984 3
你可以像这样调用 plot3d()
:
You can call plot3d()
like this :
R> plot3d(df$x, df$y, df$z, col=df$color, size=2, type='s')
哪个会给你类似的东西:
Which will give you something like :
这篇关于带有 R 和 Rgl 的彩色球体的 3d 散点图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!