简体   繁体   English

R中使用rgl plot3d的三维散点图 - 每个数据点的大小不同?

[英]3d scatterplot in R using rgl plot3d - different size for each data point?

I am using 我在用

plot3d(x,y,z, col=test$color, size=4) 

to plot a 3d scatterplot of my dataset with R, but with rgl the size argument only takes one size. 使用R绘制我的数据集的三维散点图,但使用rglsize参数只需要一个大小。

Is it possible to have different sizes for each data point, perhaps with another library, or is there a simple workaround? 是否可以为每个数据点设置不同的大小,可能是另一个库,还是有一个简单的解决方法?

Thanks for your ideas! 谢谢你的想法!

Here's a work-around along the same lines suggested by Etienne. 这是Etienne建议的同样的解决方法。 The key idea is to set up the plot, then use a separate call to points3d() to plot the points in each size class. 关键的想法是设置图,然后单独调用points3d()来绘制每个尺寸类中的点。

# Break data.frame into a list of data.frames, each to be plotted 
# with points of a different size
size <- as.numeric(cut(iris$Petal.Width, 7))
irisList <- split(iris, size)

# Setup the plot
with(iris, plot3d(Sepal.Length, Sepal.Width, Petal.Length, col=Species, size=0))

# Use a separate call to points3d() to plot points of each size
for(i in seq_along(irisList)) {
    with(irisList[[i]], points3d(Sepal.Length, Sepal.Width, 
                                 Petal.Length, col=Species, size=i))
}

(FWIW, it does appear that there's no way to get plot3d() to do this directly. The problem is that plot3d() uses the helper function material3d() to set point sizes and as shown below, material3d() only wants to take a single numeric value.) (FWIW,似乎没有办法让plot3d()直接执行此操作。问题是plot3d()使用辅助函数material3d()来设置点大小,如下所示, material3d()只想取单个数值。)

material3d(size = 1:7)
# Error in rgl.numeric(size) : size must be a single numeric value

Just in case anyone else stumbles across this years later, it is now completely possible to pass a variable into the size argument, eg with(test, plot3d(x,y,z, col=test$color, size=test$size)) . 为了防止其他人在这些年后偶然发现,现在完全有可能将变量传递给size参数,例如with(test, plot3d(x,y,z, col=test$color, size=test$size))

Actually, you can conduct operations on the data being fed to size and it will also work. 实际上,您可以对输入大小的数据进行操作,它也可以工作。 I succeeded in making a basic size ratio with something along the lines of size = x/(max(x,na.rm=TRUE)-min(x,na.rm=TRUE)) . 我成功地用size = x/(max(x,na.rm=TRUE)-min(x,na.rm=TRUE))的线来制作基本尺寸比。

While it is true that material3d will only accept single numeric values, the expression or variable fed to size within the with statement should be evaluated beforehand, so material3d should still only see a single size value for each plotted point. 虽然material3d确实只接受单个数值,但是应该事先评估在with语句中输入大小的表达式或变量,因此material3d应该仍然只能看到每个绘制点的单个大小值。 Just a heads up since I spent a an hour or so fiddling with the workaround posted here before realizing it was unnecessary. 自从我花了一个多小时左右摆弄这里发布的解决方法之后才意识到这是不必要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM