首页 > 解决方案 > R中的颜色编码球体plot3d

问题描述

我有在 R 中创建球体的纬度和经度。我需要做的是根据相应的幅度为球体着色。它是 excel 文件中的 3 列:纬度、经度和振幅。所以每一行都是一个数据点。我得到的是一个统一的模式 - 它不能从幅度列中读取值。

test$x<-0.5*cos(test$lat)*cos(test$long)
test$y<-0.5*cos(test$lat)*sin(test$long)
test$z<-0.5*sin(test$lat)
test$color<-test$amplitude
ramp<- colorRamp(c("blue", "red"))((test$color)/max(test$color))
plot3d(x= test $x,y= test $y,z= test $z,xlim=c(-0.6,0.6), ylim=c(-0.6,0.6),zlim=c(-0.6,0.6),xlab = "X", ylab = "Y", zlab = "Z", col=test$color)

预期结果是根据幅度对每个 x、y、z 点进行着色。sphere_4

标签: rrgl

解决方案


您计算ramp了 ,但没有使用它。该colorRamp函数返回一个 RGB 值矩阵,不能用作 ; 中的颜色rgl。您需要使用该rgb()功能转换为颜色。例如,使用一些虚假数据:

test <- data.frame(lat = runif(2000, -89, 89)*pi/180, 
                   long = runif(2000, 0, 359)*pi/180 )
test$x <- 0.5*cos(test$lat)*cos(test$long)
test$y <- 0.5*cos(test$lat)*sin(test$long)
test$z <- 0.5*sin(test$lat)
test$amplitude <- test$z
test$color <- abs(test$amplitude)
ramp <- colorRamp(c("blue", "red"))((test$color)/max(test$color))
ramp <- rgb(ramp, maxColorValue = 255)
library(rgl)
plot3d(x=test$x, y=test$y, z=test$z, xlab = "X", ylab = "Y", zlab = "Z", col=ramp)

这将产生以下图:

截屏


推荐阅读